Articles in this section
Category / Section

How do I make the value in one combo box cell determine the drop-down list in another combo box cell?

2 mins read

 

Here is one way that you can do this. The idea is to catch the CurrentCellShowingDropDown event on the second combo box cell (the slave combo box) then, check the value of the first combo box (the master combo box), and use it to filter the list that will be displayed in the drop-down.

A couple of technical points will arise here. The datasource for the slave combo box must be the unfiltered DataTable. In the CurrentCellShowingDropDown event, a filtered dataview is created based on this DataTable and it is this DataView that is assigned as the datasource for the dropped list. Also, when you change the value in the master cell, you may want to force the slave cell to a state that will require the user to make a selection, or you may want to explicitly set some value at this point. You can use the CurrentCellChanged event for this purpose.

C#

private int MasterColumn = 2;

private int SlaveColumn = 3;

private DataTable slaveComboTable;

//Used to immediately commit changes in the master cell

private void grid_CurrentCellChanged(object sender, EventArgs e)

{

GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;

if(cc.ColIndex == MasterColumn)

{

cc.ConfirmChanges(false);

//Reset the slave value so the user must change it...

this.gridDataBoundGrid1[cc.RowIndex, SlaveColumn].Text = "";

}

}

private void grid_CurrentCellShowingDropDown(object sender, GridCurrentCellShowingDropDownEventArgs e)

{

GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;

if(cc.ColIndex == SlaveColumn)

{

GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;

if(cr != null)

{

DataView dv = new DataView(slaveComboTable);

dv.RowFilter = string.Format("[masterId] = '{0}'", this.gridDataBoundGrid1[cc.RowIndex, MasterColumn].Text);

((GridComboBoxListBoxPart)cr.ListBoxPart).DataSource = dv;

}

}

}

VB

Private MasterColumn As Integer = 2

Private SlaveColumn As Integer = 3

Private slaveComboTable As DataTable

'Used to immediately commit changes in the master cell

Private Sub grid_CurrentCellChanged(ByVal sender As Object, ByVal e As EventArgs)

Dim cc As GridCurrentCell = Me.gridDataBoundGrid1.CurrentCell

If cc.ColIndex = MasterColumn Then

cc.ConfirmChanges(False)

'Reset the slave value so the user must change it...

Me.gridDataBoundGrid1(cc.RowIndex, SlaveColumn).Text = ""

End If

End Sub

Private Sub grid_CurrentCellShowingDropDown(ByVal sender As Object, ByVal e As GridCurrentCellShowingDropDownEventArgs)

Dim cc As GridCurrentCell = Me.gridDataBoundGrid1.CurrentCell

If cc.ColIndex = SlaveColumn Then

Dim cr As GridComboBoxCellRenderer = TryCast(cc.Renderer, GridComboBoxCellRenderer)

If Not cr Is Nothing Then

Dim dv As DataView = New DataView(slaveComboTable)

dv.RowFilter = String.Format("[masterId] = '{0}'", Me.gridDataBoundGrid1(cc.RowIndex, MasterColumn).Text)

CType(cr.ListBoxPart, GridComboBoxListBoxPart).DataSource = dv

End If

End If

End Sub

Here is a sample that illustrates this:

http://help.syncfusion.com/support/samples/kb/Grid.Windows/MasterSlaveCombo/MasterSlaveCombo.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied