I have a delete button which does this:


The problem is that if I delete row 0 (the 0th index in the DataGridView and in the underlying table, all is well. But now row 1 in the table corresponds to row 0 in the DataGridView, so if I run that code to delete a row again, nothing happens because row 0 in the table is already deleted.

I tried


after the call to delete, but neither seems to fix the problem. Of course the problem is fixed once Update is called on the DataAdapter, but I didn't want to do that until all of the changes were made, this way I could cancel the changes if the user so wishes.

Is there a way to fix this?



Recommended Answers

All 5 Replies

I see you've posted this on the vbforums website as well. I don't post over there but it doesn't look like you've gotten your answer yet either. I have two possible suggestions for you

One is to reset the row numbers with something like this

For i = 0 To newRow.ItemArray.Length - 1
      currentRow(i) = newRow(i)

( I found that on experts-exchange - if you scroll to the bottom the solutions are there but he was having a different problem than you)


The other suggestion is to look at .acceptchanges

possibly http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges.aspx and reset the database

So you can actually change the row index of a row in a table?

I mentioned I tried calling acceptchanges after deleting the row with no change, which does seem odd?

looks like it, although I have not done this myself - type"

"VB.NET WinForms - DataGridView - Adding new rows, removing old ones to AcceptChanges"

in google and it's the first entry, you can then scroll down to the bottom and see the accepted solution. (it's a long scroll down)

for some reason the accepted solution is blocked when you post the link somewhere

I got it to work... shouldn't have been this difficult to figure out.

The key was to use the filter() method of the binding source, rather than setting the DataGridView datasource to the select() method of the datatable.

PunchEventTableBindingSource.Filter = FilterString
 PunchEventTableBindingSource.Sort = SortString

Then I can delete a row like this:

DirectCast(PunchEventTableBindingSource.Current, DataRowView).Delete()

Hmm, thanks for posting your solution. That should help

would you mind marking it as solved so others can find the answer?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.