Combobox Column with different Lists

Combobox Column with different Lists

Old forum URL: forums.lhotka.net/forums/t/3025.aspx


josh.kodroff posted on Tuesday, June 12, 2007

I have a list of objects I want to display in a DataGridView.  Each list has a field called ComponentType (read-only, not displayed) and another called InitialStatus.  The possible values for InitialStatus, held in a property called InitialStatusList depends on ComponentType.

Here's some examples to illustrate:
If ComponentType = 1, then the possible values are:
A
B
C

If ComponentType = 2, then the possible values are:
B
C
D

I'm going to list Components of various types in the same datagrid.  How do I set the datasource for the combobox column where I set InitialStatus?  Do I have to write code to handle the OnSelectionChanged event?

dcleven replied on Tuesday, June 12, 2007

Hi,

This is what I have found to work.

Since the DataGridView column bindings affect all the rows, and all rows must have a valid lookup for the combobox that you are binding, you must initially have a bindingsource that is valid for all combinations. In other words in your form load event set the combobox.DataSource to a list that contains all possible InitalStatus values.

Then on the combobox BeginEdit event you must set the DataSource for the Cell to a filtered view that contains only those values that match your ComponentType. Like this:

DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)myDataGridView.CurrentRow.Cells[this.InitialStatusColumn.Index];

dgcb.DataSource = filteredInitalStatus;

--Doug

 

 

Copyright (c) Marimer LLC