Hi there,
I'm loading a RadGrid with AutoGenerateColumns="true", for databinding I'm using NeedDataSource(...) event handler. Since I want to format values from some columns I use the ColumnCreated(...) event handler. I also want to do some Aggregation based on the type of column. For this, I have the following helper method:
public static void ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
{
if(!string.IsNullOrEmpty(e.Column.UniqueName))
{
GridBoundColumn gbc = e.Column as GridBoundColumn;
if (gbc != null)
{
gbc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
gbc.HeaderText = gbc.UniqueName.ToUpper() == "EXPANDCOLUMN" ? string.Empty : gbc.UniqueName;
switch (gbc.DataTypeName.ToUpper())
{
case "SYSTEM.DATETIME":
gbc.DataFormatString = "{0:d}";
break;
case "SYSTEM.DECIMAL":
gbc.DataFormatString = "{0:N2}";
gbc.Aggregate = GridAggregateFunction.Sum;
gbc.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gbc.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
break;
}
}
}
}
But when this event fires something real "weird" is happening. I keep getting the following error:
Microsoft JScript runtime error:
Sys.WebForms.PageRequestManagerServerErrorException: cannot find column [xxxxxx].
When I mean "weird" is that such column does exist!.
On the other hand, when I comment out the logic inside ColumnCreated(...) event, the error does not show up. But as you might expect... the values are not formatted as well as there is no Aggregation on the columns that I want, and I do really want the event to fire since I need to do some formatting on the values of certain columns.
Thanks in advance,
Felix
Today, I tried a different approach. This time I used the ItemDataBound(...) event.
The code now is as follows:
protected void gvwOperaciones_ItemDataBound(object sender, GridItemEventArgs e)
{
if(e.Item.ItemType == GridItemType.AlternatingItem ||
e.Item.ItemType == GridItemType.Item)
{
int colIndex = 0;
DataRowView drv = e.Item.DataItem as DataRowView;
foreach (DataColumn dc in drv.DataView.Table.Columns)
{
GridDataItem gdi = e.Item as GridDataItem;
switch (dc.DataType.ToString().ToUpper())
{
case "SYSTEM.DATETIME":
gdi[dc.ColumnName].Text = string.Format("{0:d}", drv[dc.ColumnName]);
break;
case "SYSTEM.DECIMAL":
gdi[dc.ColumnName].Text = string.Format("{0:N2}", drv[dc.ColumnName]);
GridBoundColumn gbc = gvwOperaciones.MasterTableView.AutoGeneratedColumns[colIndex] as GridBoundColumn;
if(gbc != null)
gbc.Aggregate = GridAggregateFunction.Sum;
break;
default:
break;
}
colIndex++;
}
}
}
But something interesting again!!!, this time if I comment out the lines:
GridBoundColumn gbc = gvwOperaciones.MasterTableView.AutoGeneratedColumns[colIndex] as GridBoundColumn;
if(gbc != null)
gbc.Aggregate = GridAggregateFunction.Sum;
It works! but remember that the aggregation Does not occur!!!
Now as soon as I put them back again, the aforementioned error in javascript pops up again!!!
Question:
Is it bug in RadGrid? Am I not coding the right way?
This is really driving me nuts! :-( Can some one, please shed some light on this?
Felix