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.)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 2
<Columns>
...
<asp:BoundField DataField="Price" HeaderText="Price"
ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80" />
...
</Columns>
Method 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;
}
}
}
Method 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;}
Method 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
Method 6
Did you add this in the first line of the GridView?
OnRowDataBound="GridView1_RowDataBound"
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0