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

[Solved] Invalid cast exception in nodedatabound event

3 Answers 182 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Darren166
Top achievements
Rank 1
Darren166 asked on 01 Mar 2008, 12:39 AM
I am getting the above when trying to cast a datarow returned in a nodedatabound events' radtreenodeeventargs to a strongly typed datarow derived from the strongly typed datatable that the treeview was originally bound to.

To reproduce, just create a strongly typed dataset with one table, add a prometheus treeview to a page with an object datasource bound to the control that derives from the table you added to the dataset, then hook up the nodedatabound event for the control.

If you then create a datarow and try to cast it back to your strongly typed object it fails.

eg

protected
void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e)
{
DataRow datarow = ((DataRowView)e.Node.DataItem).Row;
MyDataset.MyDataRow myrow = (MyDataset.MyDataRow)datarow; 

Can someone please help me with this.

Darren

3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 03 Mar 2008, 08:57 AM
Hi Darren166,

I couldn't reproduce that problem. Please find attached a sample web site. Let me know if I am missing something.

Regards,
Albert
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Darren Hall
Top achievements
Rank 1
answered on 03 Mar 2008, 12:32 PM
(On my normal login now, had to temporarily create the other one as I was away from my usual pc)

Thanks for the above sample, that works fine.  However, try changing it as follows;

Instead of using the category table, use the employees table.  Set up treeview as follows;

<telerik:RadTreeView runat="server" ID="RadTreeView1" DataSourceID="ObjectDataSource1" LoadingStatusPosition="BeforeNodeText" OnNodeDataBound="RadTreeView1_NodeDataBound" DataFieldID="EmployeeID" DataFieldParentID="ReportsTo" DataNavigateUrlField="LastName" DataTextField="FirstName" DataValueField="EmployeeID">
<CollapseAnimation Duration="100" Type="OutQuint" />
<ExpandAnimation Duration="100" Type="OutQuart" />
</telerik:RadTreeView>

in your code behind, change the code as follows;

protected void RadTreeView1_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
{
DataSet1.EmployeesRow row = (DataSet1.EmployeesRow)(((DataRowView)e.Node.DataItem).Row);
e.Node.Text = row.FirstName + " " + row.LastName;
}

Now when you run the sample you should get the invalid cast exception.  It seems to stem from setting the DataFieldParentId.  If you remove that setting from the treeview above it works fine.

Darren
0
Atanas Korchev
Telerik team
answered on 03 Mar 2008, 01:24 PM
Hello Darren Hall,

Indeed when binding using DataFieldID/DataFieldParentID RadTreeView creates a temporary DataSet to create it s hierarchy. The code we are currently using seems to "lose" the type of the datatable. Fortunately we were able to fix that limitation. Please open a support ticket so we can send you the hotfix. Thanks.

Regards,
Albert
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
TreeView
Asked by
Darren166
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Darren Hall
Top achievements
Rank 1
Share this question
or