Question

I'm stuck to an issue that usually I wouldn't even face, but in this case it seems a big deal.

First of all, to avoid confusion, even though I've already written it in the title, we're talking about Windows Forms (not WPF, neither web related applications).

I've a DataGridView whose datasource comes from the navigation property of an entity (entity Framework 5, Code First).

This datagrid has, among other fields, an "IdEquipment" and an "IdConfiguration". Both columns are of type DataGridViewComboBoxEditingControl. IdEquipment is populated by a BindingSource control, that "Equipment":

 _context.Equipments.Local.ToBindingList();

The "Configuration" table as a foreign key "IdEquipment" and the combobox must how a subset of this table in each line of the datagrid. To do that, after some unsuccesful tests and searching here in SO and somewhere else, I've found this solution, whith a particular focus on dataGridView1_EditingControlShowing and dataGridView1_CellValueChanged.

It seems to be the right direction, however, the problem is when I filter the datasource of IdEquipment combobox (that is also another BindingSource control bound to _context.Configurations.Local.ToBindingList();.

I've read somewhere that since I'm working with entities, I'm supposed to call .ToList() rather than .ToBindingList(), but even in this case, filtering doesn't happen (and anyway I think that a BindingList is still fine).

Another option I could use is to take advantage of the selected value/entity of the first combobox, and get the Equipment.Configuration navigation property, but I have no idea of how to do that with a DataGridViewComboBoxEditingControl.

Any help?

Was it helpful?

Solution

After struggling some more hours, I've eventually found the solution that best applies to my needs: I don't know if this is THE solution though.

  • Got rid of all BindingSource controls used by the combo boxes.
  • On DataGrid DataBindingComplete event: configured the data source of both combo with a non filtered List and set DisplayMember and DisplayValue properties (both list have a first "null" element).
  • On DataGrid EditingControlShowing event: changed the datasource of the second combo with a filtered List based on the current value of IdEquipement (if any) and keeping the first null element.
  • On DataGrid CellValueChanged event: if the changed column id IdEquipement, se to null the value of the column containing IdConfiguration

This, so far, works. I hoped in a easier way to achieve the same result, but as far as I can see there are no options.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top