I have a DataGridView that is bound to a DataSet and I allow someone to remove a row from the table and I remove the row from the set. My code looks like this:
ds.Tables(0).Rows(dgCourseList.SelectedRows(0).Index).Delete()
My problem I found is if the grid gets sorted the index of the rows no longer corresponds to the indexes in the DataSet. As a result the wrong rows get removed from the set.
Can I solve this problem with maybe having to sort the DataSet on some sort event of the DataGridView?
I haven’t tested this but try the following assuming you have the
DataKeyssetup properly:EDIT: Based on you specifying that you are using a
DataGridViewwhich is a winforms app (I was assuming a webforms app) then you could try:EDIT 2: Updated because I think the cast was wrong… still untested though.