GridView Row Deleting
-
Here am trying to delete the row in the dataset for Delete button click on each row and finally, when I click SAVE button, the entire dataset will be pushed into the db. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataSet dsGr = (DataSet)Cache["MyGrid"]; int RowID = (int)GridView1.DataKeys[e.RowIndex].Value; dsGr.Tables[0].Rows[RowID].Delete(); Cache["MyGrid"] = dsGr; GridView1.DataSource = dsGr; GridView1.DataBind(); } The DataKeyNames property for gridview is set to "id". When I delete the first row on the grid, the RowID=1 and hence the first row on the dataset gets deleted but actually the first row (0th row) should be deleted. Can you please suggest if am going wrong on identifying the RowID? int RowID = (int)GridView1.DataKeys[e.RowIndex].Value; Just guide me how to find the correct RowID Thanks.
-
Here am trying to delete the row in the dataset for Delete button click on each row and finally, when I click SAVE button, the entire dataset will be pushed into the db. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataSet dsGr = (DataSet)Cache["MyGrid"]; int RowID = (int)GridView1.DataKeys[e.RowIndex].Value; dsGr.Tables[0].Rows[RowID].Delete(); Cache["MyGrid"] = dsGr; GridView1.DataSource = dsGr; GridView1.DataBind(); } The DataKeyNames property for gridview is set to "id". When I delete the first row on the grid, the RowID=1 and hence the first row on the dataset gets deleted but actually the first row (0th row) should be deleted. Can you please suggest if am going wrong on identifying the RowID? int RowID = (int)GridView1.DataKeys[e.RowIndex].Value; Just guide me how to find the correct RowID Thanks.
-
Should be: int RowID = e.RowIndex; This gets the correct Index for the row that fired the event.