.NET GridView - Can you right-align just one column?

后端 未结 6 1821
夕颜
夕颜 2021-01-02 01:21

Can you easily right-align just one column in a GridView?

I have this


         


        
6条回答
  •  鱼传尺愫
    2021-01-02 02:21

    Yes, you can, but I think if you have AutoGenerateColumns set to true (which it is by default) then you need to right align the column using the RowDataBound event. As a side note, if it's easier you can set AutoGenerateColumns to false and use BoundFields which will give you more formatting options and will probably eliminate the need for the RowDataBound event.

    GridView:

    
    

    Codebehind:

    protected void GridView1_RowDataBound(object o, GridViewRowEventArgs e)
    {
        //Assumes the Price column is at index 4
        if(e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
    }
    

    Hope that helps.

提交回复
热议问题