This is a migrated thread and some comments may be shown as answers.

InvalidCastException for Dynamically Created Calculated Column

2 Answers 146 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tlp
Top achievements
Rank 1
tlp asked on 30 Nov 2010, 04:29 PM

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

2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 06 Dec 2010, 10:20 AM
Hello Tony,

This would work with any version of RadControls for ASP.NET AJAX provided that you either use assemblies built towards .NET 2.0 or you set the EnableLinqExpressions property to false.
private void InitializeGrid()
{
    GridBoundColumn boundColumn;
 
    RadGridData = new RadGrid();
    RadGridData.EnableLinqExpressions = false;
 
    // Set required event handlers.

Let me know if you need more information.

Best regards,
Daniel
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
tlp
Top achievements
Rank 1
answered on 07 Dec 2010, 03:47 PM
Thank-you. That worked great.


Tony
Tags
Grid
Asked by
tlp
Top achievements
Rank 1
Answers by
Daniel
Telerik team
tlp
Top achievements
Rank 1
Share this question
or