I currently upgraded to the 2010 Q3 ASP.NET AJAX controls from 2009.3.1314 and have encountered an issue which I did not previoulsy have. Basically, when the grid columns are created dynamically and a calculated column is included, the following exception is encountered in the ItemDataBoundEvent for the grid:
System.InvalidCastException
{"Unable to cast object of type 'DynamicClass1' to type 'DataXY'."}
The exception occurs in the ItemDataBound event when the DataItem is cast back to its original type. In my previous version, I was able to perform the cast. Provided is sample code that will reproduce the error.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Telerik.Web.UI; public partial class Default2 : System.Web.UI.Page { RadGrid RadGridData; override protected void OnInit(EventArgs e) { // Call base init. base.OnInit(e); InitializeGrid(); } protected void Page_Load(object sender, EventArgs e) { } private void InitializeGrid() { GridBoundColumn boundColumn; RadGridData = new RadGrid(); // Set required event handlers. RadGridData.NeedDataSource += new GridNeedDataSourceEventHandler(RadGridData_NeedDataSource); RadGridData.ItemDataBound += new GridItemEventHandler(RadGridData_ItemDataBound); // Set grid properties. RadGridData.ID = "GridDataId"; RadGridData.AutoGenerateColumns = false; // Required for row highlighting. RadGridData.ClientSettings.Selecting.AllowRowSelect = true; RadGridData.MasterTableView.AllowPaging = true; RadGridData.MasterTableView.PageSize = 8; RadGridData.MasterTableView.PagerStyle.Mode = GridPagerMode.NextPrev; RadGridData.MasterTableView.PagerStyle.Position = GridPagerPosition.Top; RadGridData.MasterTableView.Name = "GridData"; RadGridData.MasterTableView.DataMember = "DataXY"; // *** Grid columns. *** boundColumn = new GridBoundColumn(); RadGridData.MasterTableView.Columns.Add(boundColumn); boundColumn.UniqueName = "XId"; boundColumn.DataField = "X"; boundColumn.HeaderText = "X"; boundColumn = new GridBoundColumn(); RadGridData.MasterTableView.Columns.Add(boundColumn); boundColumn.UniqueName = "YId"; boundColumn.DataField = "Y"; boundColumn.HeaderText = "Y"; // *** Grid Calculated Column *** GridCalculatedColumn calculatedColumn = new GridCalculatedColumn(); RadGridData.MasterTableView.Columns.Add(calculatedColumn); calculatedColumn.UniqueName = "CalculatedColumn1"; calculatedColumn.DataFields = new string[] { "X", "Y" }; calculatedColumn.Expression = "{0} + {1}"; calculatedColumn.Visible = true; calculatedColumn.HeaderText = "X + Y"; PlaceHolder1.Controls.Add(RadGridData); } void RadGridData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) { RadGridData.MasterTableView.DataSource = this.GetGridData(); } void RadGridData_ItemDataBound(object sender, GridItemEventArgs e) { if (!(e.Item is GridDataItem)) return; GridDataItem item = e.Item as GridDataItem; DataXY dataXY = (DataXY)item.DataItem; } private List<DataXY> GetGridData() { List<DataXY> list = new List<DataXY>(); list.Add(new DataXY(1, 1)); list.Add(new DataXY(2, 2)); list.Add(new DataXY(3, 3)); list.Add(new DataXY(4, 4)); list.Add(new DataXY(5, 5)); list.Add(new DataXY(6, 6)); list.Add(new DataXY(7, 7)); return list; } private class DataXY { public DataXY() { } public DataXY(int x, int y) { this.X = x; this.Y = y; } public int X { get; set; } public int Y { get; set; } } }
Any help in resolving this issue would be greatly appreciated.
Thanks,
Tony