Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Right-Align Columns on GridView control.

Status
Not open for further replies.

tadd

Programmer
Oct 28, 2001
70
US

I have a GridView control on a web page that I bind to a DataSet. I want to right-align the data in the columns that contain numeric data. I would think that this should be as simple as setting a property on a column, but apparently Microsoft wants to keep this a secret, so I am wasting a whole afternoon on it.

Can anyone point me in the right direction? Thanks.

What is weird is that when I trace through the code, after binding to the DataSet, the Columns.Count property on the GridView shows 0. (but yes the data rows DO appear on the page so I know the data are binding).

I am willing to admit that I am the dumb one here. Any help is appreciated. Thanks.
 

I found the bit of code shown below - which it should work for me - but it does not. As I noted in my first post above, my GridView control does not seem to have any Columns - even tho I can see the data in the grid (huh?). In other words, referring to GridViewCntrl.Columns[0] generates an error. There must be some basic concept that I am missing here.

=======================================
GridViewCntrl.Columns[0].ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
=======================================
 
I am using ASP.NET and C#. The GridView in on a web page.

I figured out how to do it. Now that I know what I know, I should have mentioned that I am binding data with the AutoGenerateColumns property set to TRUE on the grid. As it turns out, you have to set the cell properties on the fly as the data is binding. This is done in the RowDataBound event like so:


===================================================

protected void myGridView_RowDataBound(object o, GridViewRowEventArgs e)
{
// Set the alignment of cells containing numeric data.
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
}
}
===================================================

I hope that helps somebody. It sure took me a long time to figure it out.

 
Glad you got it :) Just wanted to make sure it was ASP.Net before I told you how to do it :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top