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

Easiest Way To Delete An Item From RadGrid

2 Answers 627 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nate
Top achievements
Rank 1
Nate asked on 09 Dec 2014, 09:30 PM
I am trying to delete a item from my RadGrid. I have read a lot of posts, and documentation. No matter what I try, I can't delete an item.

I set my database from the code behind. I have the properties AllowAutomaticDeletes and AutoGenerateDeleteColumn set to true.

Is there a easy way when the user click the delete link, it remove that record?

2 Answers, 1 is accepted

Sort by
0
Nate
Top achievements
Rank 1
answered on 10 Dec 2014, 07:19 PM
This is my code:

protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            var cmd = new SqlCommand("select Id AS Item, Description, Cost AS Price from " + ClassOrderSession.strTableName + "", con);

            var da = new SqlDataAdapter(cmd);
            var dt = new DataTable();

            da.Fill(dt);                               
           
            string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
          
            if (dt.Rows.Find(ID) != null)
            {
                dt.Rows.Find(ID).Delete();
                dt.AcceptChanges();
                Session["DataSource"] = dt;
            }

The code always errors on string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
I get the error:

An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
0
Konstantin Dikov
Telerik team
answered on 12 Dec 2014, 02:20 PM
Hello Nate,

Automatic CRUD operations will work only when you bind the grid to declarative DataSource control. Detailed information on this matter could be found in the following help article:
As for the error that you are receiving, can you please confirm that you have included your "id" data field in the MasterTableView DataKeyNames collection:
<MasterTableView DataKeyNames="id" ...

I assume that you have following our help article for Deleting Records, but please ensure that you are following the exact same steps, as in the example.

Hope this helps


Best Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Nate
Top achievements
Rank 1
Answers by
Nate
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or