user42348
user42348

Reputation: 4319

How do I delete a row from a GridView during the RowDeleting event?

I have a GridView and its DataSource is datatable.OnRowDeleting event of Gridview. I can't seem to delete that row when I click the 'delete' button that invokes the event to remove the row.

My code is like this in RowDeleting event of GridView:

DataTable.Rows.Remove(GridView.DataKeys(e.rowindex).value)

I get the error:

"Cannot type cast from System.Int32 To System.Data.DataRow"

Does anyone have any suggestions?

Upvotes: 0

Views: 16023

Answers (1)

TheTXI
TheTXI

Reputation: 37915

You can find the row to delete inside the RowDeleting event by looking at the

e.RowIndex

You can then use the e.RowIndex to find the proper row in your DataTable, delete that row, and then refresh you gridview by calling .databind() on it once again to that DataTable.

Example:

DataTable1.Rows(e.RowIndex).Delete()
GridView1.DataSource = DataTable1
GridView1.DataBind()

Upvotes: 4

Related Questions