?
Solved

Center text in a dynamically generated GridView column

Posted on 2006-11-18
4
Medium Priority
?
2,089 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:kraffay
kraffay earned 400 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

770 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