Click here to Skip to main content
14,427,367 members
Rate this:
Please Sign up or sign in to vote.
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:

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 10-Jan-20 0:53am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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?

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 :)
   
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.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100