styling Columns when using AutoGenerateColumns

T

Tim_Mac

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
 
L

Luke Zhang [MSFT]

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.
 
T

Tim_Mac

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
 
L

Luke Zhang [MSFT]

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.
 
T

Tim_Mac

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top