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

Unable to cast object of type 'Telerik.WebControls.GridCommandItem' to type 'Telerik.WebControls.GridDataItem'

2 Answers 84 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Abiri Masoni
Top achievements
Rank 1
Abiri Masoni asked on 07 Jan 2010, 09:39 AM
Hello all!

I have the code below which generates the above error on the line

GridDataItem item = (GridDataItem) e.Item;

 

 

 



 

protected void ViewStudentGrid_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)

 

{

 

//Get the GridDataItem of the RadGrid

 

 

GridDataItem item = (GridDataItem) e.Item;

 

 

//Get the primary key value using the DataKeyValue.

 

 

Guid gdStudId = new Guid(item.OwnerTableView.DataKeyValues[item.ItemIndex]["StudId"].ToString());

 

 

try

 

{

 

DeleteClass.DeletePeriods(gdStudId);

 

 

//ParameterManager.CourseTypeDelete(CourseID);

 

}

 

catch (Exception ex)

 

{

ViewStudentGrid.Controls.Add(

new LiteralControl("Unable to Delete Record. Reason: " + ex.Message));

 

e.Canceled =

true;

 

}

}


and yet in this code below, there's no problem with the casting

 

protected

 

void PeriodsRadGrid_DeleteCommand(object source, Telerik.WebControls.GridCommandEventArgs e)

 

{

 

//Get the GridDataItem of the RadGrid

 

 

 

 

 

GridDataItem item = (GridDataItem)e.Item;

 

 

//Get the primary key value using the DataKeyValue.

 

 

 

 

 

Guid gdPeriodId = new Guid(item.OwnerTableView.DataKeyValues[item.ItemIndex]["PeriodId"].ToString());

 

 

try

 

 

 

 

{

 

DeleteClass.DeletePeriods(gdPeriodId);

 

 

//ParameterManager.CourseTypeDelete(CourseID);

 

 

 

 

}

 

catch (Exception ex)

 

{

StudyPeriodsGrid.Controls.Add(

new LiteralControl("Unable to Delete Record. Reason: " + ex.Message));

 

e.Canceled =

true;

 

}

}


What could be the difference in the Telerik.WebControls.GridCommandEventArgs e in the two cases

Thnx so much

2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 08 Jan 2010, 12:24 PM
Hi Abiri,

I went through the provided code and I can confirm the cast is both event handlers is a valid one. However note that the ItemCommand event of RadGrid is fired when any command button is clicked. And if you clicked on a button in the GridCommandItem, then the e.Item argument is of type GridCommandItem. Therefore I suggest that you first check which command fired the event and then place you code accordingly.

Sincerely yours,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Abiri Masoni
Top achievements
Rank 1
answered on 08 Jan 2010, 12:41 PM
Thnx for the response., though i'd already sorted myself thru that problem having seen where i was going wrong.
Tags
General Discussions
Asked by
Abiri Masoni
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Abiri Masoni
Top achievements
Rank 1
Share this question
or