Centering the data in a cell in grid view
I have a gridview and i'm associating the datasource for the grid view dynamically at runtime
I mean
gridView2.DataSource = titlesArrayList;
gridView2.DataBind();
And i want the data in cells of this grid view to be centered, right now they are left-justified
If i was associating data through
<asp:BoundField />
I have this prop开发者_如何学JAVAerty
ItemStyle-HorizontalAlign="Center"
But how can i access this property if the gridview is associated dynamically
I hope u understand my problem
Thanks in anticipation
EDIT:
try
foreach (GridViewRow row in GridView1.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.Attributes.CssStyle["text-align"] = "center";
}
}
The easiest way is probably from the mark-up. Just assign HorizontalAlign
to Center in the grid's RowStyle
. No need to iterate through every cell programmatically.
Example:
<asp:GridView>
<RowStyle HorizontalAlign="Center" />
<Columns>
...
</Columns>
</asp:GridView>
You can add below style block before the GridView
<style>
td, th {
text-align: center;
}
</style>
精彩评论