Solved

Center text in a dynamically generated GridView column

Posted on 2006-11-18
4
2,075 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now