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

3 posts, 0 answers
  1. Abiri Masoni
    Abiri Masoni avatar
    30 posts
    Member since:
    Dec 2009

    Posted 07 Jan 2010 Link to this post

    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. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 08 Jan 2010 Link to this post

    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.
  3. DevCraft banner
  4. Abiri Masoni
    Abiri Masoni avatar
    30 posts
    Member since:
    Dec 2009

    Posted 08 Jan 2010 Link to this post

    Thnx for the response., though i'd already sorted myself thru that problem having seen where i was going wrong.
Back to Top