InvalidOperationException on Databinding when removing last item in list
Asked Answered
G

2

6

I'm getting the following message when I try to remove the last item in a datagridview.

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

I have my binding setup as follows.

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

ExtendedBindingList is just something simple I threw together to implement sorting and filtering and some basic state persistence. dgv is the DataGridView. GetDataFromDB returns a List of MyClass.

The issue only arises when I try to remove the last item from the datagridview using:

bs.RemoveCurrent();

which works at all other times. My only hint for a solution is to remove all bindings and reapply them but this doesn't seem like an ideal solution.

EDIT The exception only gets thrown after the BindingList successfully removes the last item in question. It get's thrown in external code so I can't tell exactly what is throwing it.

So, here I am, asking SO for some help :).

Thanks in advance, Justin

Granulate answered 29/4, 2009 at 21:46 Comment(0)
C
0

Here is how I remove selected row from a 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);
  }
}

groups is my BindingListEx(Of T).

Hope it helps.

Cybernetics answered 29/4, 2009 at 21:53 Comment(7)
I'm using a DataGridView not a Grid, but that is essentially what I'm using as well. It works every time except when I remove the last item.Granulate
It is a DataGridView in my example. BindingSource is also used, but I delete from BindingListEx, not from BindingSource. Just try this code. I also think that your problem is somehow related to the last row which is for fast adding rows (AllowUserAddNewRows = true). I remember I had a problem with it and then I decided to turn it off (set AllowUserAddNewRows to false).Cybernetics
Same exact error. It's happening in CurrencyManager.FindGoodRow(). which is marked as 'external code' in the call stack. I also have AllowUserAddNewRows set to false.Granulate
Try calling bs.SuspendBinding() before deleting the row and bs.ResumeBinding() after deleting the row.Cybernetics
Some, yes. The form is setup in a Master-Detail design. There are some comboboxs that get filled from another datasource and I need to do manual binding on those.Granulate
SuspendBinding and ResumeBinding did nothing to help.Granulate
And also can you show the whole code with bindings and setting up the datasources.Cybernetics
F
0

[Sorry, not really an answer but I feel this is valuable since no answer was given.]

I was getting the exact same situation using .NET Compact Framework 2.0. Testing traced it to the point where a NumericUpDown.DataBindings.Add() was used to bind the control to the source. After this point, using RemoveCurrent() would produce the error if the item was the last one in the source. Prior to that binding (or if it was skipped), the error would never appear.

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

Footslog answered 15/10, 2013 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.