Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview, and i bind that with datatable. i want to write code for gridview_rowdeleting event, when i click on delete link of gridview row, this row should remove from datatable, and when click another row delete, then both current and previous row should delete from datatable. i try a lot but i cannot fix the problem. please help me.
thanks in advance.
below is my code:

C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           int intforumid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
           int ii = e.RowIndex;
           lst.Add(ii);
          // ViewState["ind"] = ViewState["ind"] + e.RowIndex.ToString() + ",";
           ViewState["ind"] = ViewState["ind"] + e.RowIndex.ToString() + ",";
           List<int> del = new List<int>();

           string[] str = ViewState["ind"].ToString().Split(',');
           for (int k = 0; k < str.Length; k++)
           {
               if (str[k] != "")
               {
                   //foreach (DataRow d in dt.Rows)
                   //{
                   //    if (Convert.ToString(d["id"]) == str[k])
                   //    {
                   del.Add(Convert.ToInt32(str[k]));
                   // dt.Rows.RemoveAt(Convert.ToInt32(str[k]));
                   // dt.AcceptChanges();
                   //    }
                   //}
               }
           }
           int i1 = 0;
           for (int s = 0; s < del.Count; s++)
           {
               i1 = s;
               dt.Rows.RemoveAt(i1++);
               dt.AcceptChanges();
           }
           GridView1.DataSource = null;
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
Posted
Updated 9-Jan-20 23:53pm

1 solution

You have written too much of stuffs to do a simple delete, not sure why.
Can you try following code by just commenting out your RowDeleting event for sometime and check if it works for you?

C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
    DataTable dt = ViewState["dt"] as DataTable;  //datatable stored previousely in viewstate
    dt.Rows[index].Delete();
    dt.AcceptChanges();
    ViewState["dt"] = dt;  //save the new values back to the viewstate
    GridView1.DataSource = dt;
    GridView1.DataBind();
}


Note: This will delete the row from the datatable not from the database, for which you need to write the logic further.

Hope, it helps :)
 
Share this answer
 
v2
Comments
Member 10371658 7-Dec-15 12:32pm    
thanks dear it work fine. according to my need it's working fine.
thanks a lot.
Suvendu Shekhar Giri 7-Dec-15 12:41pm    
Glad that it helped :)
Please mark as answer so that other can take reference of it.
SDK Channel 17-Jul-20 2:52am    
hello friend, i following your code but i have a error in dt.Rows[index].Delete();
please help me. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900