robjay
asked on
Center text in a dynamically generated GridView column
I need to center my text in my GridView columns but they are created dynamically so I am not able to specificy within a <Column> specification.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is syntax that worked for me:
<asp:BoundField DataField="name" HeaderText="Name" ItemStyle-HorizontalAlign= "Center" />
<asp:BoundField DataField="name" HeaderText="Name" ItemStyle-HorizontalAlign=
ASKER
<asp:GridView ID="GridView5" Runat="server"
AutoGenerateColumns="True"
<alternatingrowstyle backcolor="#EEEEEE" />
<rowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-italic="true"
HorizontalAlign = "center" />
</asp:GridView>
so I can do this with a DataGrid and OnItemDataBound program that references my desired centered columns - all columns except the first one - but I would like to know how to implement this with a GridView. This is the DataGrid OnItemDataBound:
Protected Sub dg5_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.
' This just centers data - is there a more efficient way?
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
For k = 1 To columncount - 1 ' Do no center first column
e.Item.Cells(k).Horizontal
Next
End If
End Sub