Solved

Center text in a dynamically generated GridView column

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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