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

跟風遠走 提交于 2019-12-18 14:55:30

问题


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

I have this

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

It is bound to a DataTable (generated dynamically) that has many columns. I just want the 'Price' column to be right-aligned.

(Coming across this problem, I am wondering if I should be printing out HTML <table> instead of using a GridView. Using HTML I would have total control.)


回答1:


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:

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server"></asp: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.




回答2:


<Columns>
...
    <asp:BoundField DataField="Price" HeaderText="Price" 
        ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80" />
...
</Columns>



回答3:


Even though the question posted long ago, this might help someone you happens to end up at this page.

The answers given assume that the index of the column to which the alignment will be applied is known in advance or the columns are created at design time on the .aspx page; but this is not always the case.

For someone looking for a general solution in which the columns are auto generated and the index of column with header ‘Price’ not known in advance, here is a solution

protected void grData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int i = ((DataTable)((GridView)sender).DataSource).Columns.IndexOf("Price");
        for (int j = 0; j < e.Row.Cells.Count; j++)
        {
            if (j == i)
                e.Row.Cells[j].HorizontalAlign = HorizontalAlign.Right;
            else
                e.Row.Cells[j].HorizontalAlign = HorizontalAlign.Left;
        }
   }
}



回答4:


Enclose the item in the ItemTemplate in a div and then set styling to the div.

<ItemTemplate>
<div id="divReportName">
<asp:Label ID="lblReport" runat="server" ></asp:Label>
</div>
</ItemTemplate>

// css for div
#divReportName { text-align: left;}



回答5:


You can perform alignment within the Boundfield using ItemStyle-

like this :

<asp:BoundField DataField="SOH" HeaderText="SOH" SortExpression="SOH" ItemStyle-HorizontalAlign="Right"/>

This worked for me where i needed to align only specific columns in my gridview




回答6:


Did you add this in the first line of the GridView?

OnRowDataBound="GridView1_RowDataBound" 


来源:https://stackoverflow.com/questions/5644097/net-gridview-can-you-right-align-just-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!