Link to home
Start Free TrialLog in
Avatar of robjay
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
Avatar of kraffay
kraffay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robjay
robjay

ASKER

OK, this works but I only needed certain columns centered . . .

<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.DataGridItemEventArgs)
                    ' This just centers data - is there a more efficient way?                            
                    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                        For k = 1 To columncount - 1 ' Do no center first column
                            e.Item.Cells(k).HorizontalAlign = HorizontalAlign.Center
                         Next
                      End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robjay

ASKER

This is syntax that worked for me:

<asp:BoundField DataField="name" HeaderText="Name" ItemStyle-HorizontalAlign="Center"   />