InvalidOperationException to bind data when deleting the last item in a list

I get the following message when I try to delete the last item in a datagridview.

DataBinding cannot find a row in the list that is suitable for all bindings.

I have a snap setting as follows.

ExtendedBindingList<MyClass> bl = new ExtendedBindingList<MyClass>(GetDataFromDB()); BindingSource bs = new BindingSource(); bs.DataSource = bl; dgv.DataSource = bs; 

The ExtendedBindingList is just something simple I put together to sort and filter and some basic state persistence. dgv is a DataGridView. GetDataFromDB returns a list of MyClass.

The problem only occurs when trying to remove the last item from a datagridview using:

 bs.RemoveCurrent(); 

which works at any other time. My only hint of a solution is to remove all the bindings and reapply them, but that doesn't seem like the perfect solution.

EDIT An exception occurs only after the BindingList successfully deletes the last item in question. It gets into external code, so I can’t say for sure what throws it.

So here I am, asking for help :).

Thanks in advance, Justin

+6
c # data-binding winforms bindinglist bindingsource
source share
2 answers

This is how I remove the selected row from the grid:

 private void btnDelete_Click(object sender, EventArgs e) { if (grid.CurrentRow == null) return; var selectedItem = grid.CurrentRow.DataBoundItem as PartGroup; if (selectedItem != null && UIHelper.ShowQuestion("Are you sure you want to delete selected row?") == System.Windows.Forms.DialogResult.Yes) { groups.Remove(selectedItem); } } 

is my BindingListEx (Of T).

Hope this helps.

0
source share

[Sorry, not quite the answer, but I think it is valuable because there was no answer.]

I was getting exactly the same situation using the .NET Compact Framework 2.0. Testing tracked it to the point where NumericUpDown.DataBindings.Add () was used to bind the control to the source. After this point, using RemoveCurrent () will fail if the item was the last in the source. Prior to this binding (or if it was skipped), an error will never appear.

Other controls were tied to the same source - TextBox and ComboBox - but they did not cause this behavior. NumericUpDown control only.

0
source share

All Articles