Groups | Blog | Home
all groups > asp.net datagrid control > october 2006 >

asp.net datagrid control : styling Columns when using AutoGenerateColumns


Tim_Mac
10/3/2006 12:12:33 PM
hi,
i have an extended Datagrid class, it does nice automatic things like
providing an excel export feature, sorting, paging etc.
i would like it to automatically right-align any numeric columns, but i have
run into a problem where the 'DataGrid.Columns' collection is empty when the
DataGrid is using AutoGenerateColumns, and i am unable to set the
ItemStyle.HorizontalAlign property because there are no DataGridColumns to
access. can anyone think of a way to style the column, without resorting to
declaring every column in the ASPX?

here is the code i use in protected override void Render(...)

// align the numeric columns
int col=0;
if(this.AutoGenerateColumns)
{
// iterate over dataview table columns
foreach(DataColumn dc in this.dv.Table.Columns)
{
if(Regex.IsMatch(dc.DataType.Name, "Decimal|Double|Int16|Int32|Int64"))
this.Columns[col].ItemStyle.HorizontalAlign =
this.AutoAlignNumberColumns;
col++;
}
}

very grateful for any suggestions. if i use declared columns it does work
correctly.
thanks
tim

lukezhan NO[at]SPAM online.microsoft.com
10/5/2006 12:00:00 AM
Hello Tim,

I suppose you are working with VS.NET 2003 (since in VS.NET 2005, we use
GirdView instead DataGrid). Regaridng the issue, since you have enable the
AutoGenerateColumns property, the columns will not be added until the
datagird is bound to data. In the render method, the columns is not
generated yet. If you move these code to itemdatabound, or databinding,
will this correct the problem?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Tim_Mac
10/5/2006 12:23:19 PM
hi Luke,
thanks for the reply. yes it's a VS 2003 project, i have most of my
projects upgraded but one or two larger ones are still using 2003.

unfortunately DataGrid.Columns.Count is 0 at the start and end of the events
you describe, when AutoGenerateColumns is used. even the OnUnload event has
an empty columns collection.

just to cover the obvious question, there are two columns in the datasource
and they do render in the grid.

i wouldn't like to get into manually rendering the TR and TD tags of the
datagrid if this would have performance implications. perhaps there is an
internal method like GenerateColumns which i could reflect manually? and
then gain access to the instantiated Columns?

thanks
tim



[quoted text, click to view]

lukezhan NO[at]SPAM online.microsoft.com
10/6/2006 12:00:00 AM
Hello Tim,

Sorry for previous suggestions not working. There is no such internal
method for the "column genearation" but we may override the databind method
of the datagrid control like:

public override void DataBind()
{
base.DataBind();
// access columns here
}

Will this help?

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Hussein
11/8/2006 5:17:00 AM
Dear Tim,

U can give me the code for this project.

Thanks,
Regards,

---
Tim_Mac
11/9/2006 4:23:38 PM
hi Hussein,
that's no problem, here is the overridden DataBind event in my datagrid. my
extended datagrid class has one or two bugs that make me slow to send you
the entire class, but if you really want the whole thing that's no problem
i'll post it.
the code below uses a DataView object called dv, which is the datasource for
the grid.

public override void DataBind()
{
base.DataBind();

int col = 0;
// iterate over columns defined in grid
foreach (DataGridColumn dc in this.Columns)
{
// the sort expression is the only link between the datagrid column
and the datatable column, so we use that to access the datatype of each
column in the grid
if (dc.SortExpression != "" &&
this.dv.Table.Columns[dc.SortExpression] != null &&
Regex.IsMatch(this.dv.Table.Columns[dc.SortExpression].DataType.Name,
"Decimal|Double|Int16|Int32|Int64"))
this.Columns[col].ItemStyle.HorizontalAlign =
HorizontalAlign.Right;
col++;
}
}

hope this helps
tim


[quoted text, click to view]

AddThis Social Bookmark Button