I have a gridview and binding the records in it from a list.I need to delete multiple rows from the list
int deleled=0;
foreach(gridviewrow row in gridview1.rows)
{
checkbox chkSelectedItem=(checkbox)row.findcontrol("chkSelectedItem");
if(chkSelectedItem.checked)
{
int id=(int)gridview1.DataKeys[row.RowIndex].Values["Id"];
Person person=new Person();
person.Id=id;
deleted+=person.Delete(user);
gridview1.DeleteRow(row.rowindex);
}
}
gridview1.datasource=persons;
gridview1.databind();
here I have seperate class for deleting the user…thats working fine, I need to delete the selected records in database as well as gridview and I have to display the other records in gridview…..the problem is I’ve records for many users in Persons table, I need to display particular persons details in gridview…
the deleting records working fine,no problem in that, but when I delete the rows in gridview it skips the indexes…for example if select records 1,3,5,7,9 in the total of 10 records, when the control comes for the 1st record at 0th index, after the execution of
gridview1.DeleteRow(row.rowindex);
the 2nd record(at 1st index) fills the 0th index, and when the control comes to get the id, it gets only the 4th record instead of 3rd record….I even tried to get the selected indexes using list and tried deleting the records using their indexes in that list, that also showing the same problem, I can understand the control flow….but I dunno how to fix this….can anyone help me out
If your Persons list not very large, one way is to first unbind GridView’s data source, delete Person from Persons list and don’t delete GridView’s rows yourself then bind that list to its data source.