2
Answers

How to delete selected rows from gridview datatable.

Rahul Khanna

Rahul Khanna

9y
2.2k
1
Hi all,
I am trying to delete selected rows from gridview in button click event.My code is not deleting the correct row, if I select first row its deleting the 2nd row and If I select 2nd row its deleting the 3rd row and so on.
Here is my code:
protected void btnRemoveUser_Click(object sender, EventArgs e)
{
DataTable yourDataTable = GetDataTable(Studentlist); //CheckBox chkbx;
foreach(GridViewRow oneRow in Studentlist.Rows)
{
CheckBox chkbx = (CheckBox)oneRow.FindControl("ckbxID");
for(int i = 0; i< yourDataTable.Rows.Count; i++)
{
if (chkbx.Checked == true)
{
yourDataTable.Rows.RemoveAt(i);
}
break;
}
}
Studentlist.DataSource = yourDataTable;
Studentlist.DataBind();
}
Note: Studentlist is the gridview ID.
Here, my purpose is to delete the selected rows from gridview so any alternate solution is also appreciated.
Answers (2)
Next Recommended Forum