Rahul Khanna

Rahul Khanna

  • NA
  • 236
  • 0

How to delete selected rows from gridview datatable.

Nov 9 2014 10:08 AM
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)