Solved

Center text in a dynamically generated GridView column

Posted on 2006-11-18
4
2,078 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:robjay
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:kraffay
kraffay earned 100 total points
ID: 17973028
There's an RowStyle-HorizontalAlign property of gridview that takes a center argument.
0
 

Author Comment

by:robjay
ID: 17976044
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
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 17978724
You can align the columns with the ItemStyle for a BoundField:

                <Columns>
                    <asp:BoundField />
                    <asp:BoundField>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                </Columns>

Bob
0
 

Author Comment

by:robjay
ID: 21380880
This is syntax that worked for me:

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question