tags:

views:

1289

answers:

2

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?

+2  A: 

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()
TheTXI
How is that code?
Don't worry ramyatk06. Gustavo has given code on your other copy of this question http://stackoverflow.com/questions/665905/deleting-rows-from-datatable-in-vb-net/665931#665931
Binary Worrier
A: 

Second Answer worked thank you so much

Tryer