How do I change the DataSource of one ComboBox cell depending upon the value selected in another ComboBox cell in a GridDataBoundGrid?
The DataSource for a specific cell can be changed dynamically by handling the Model.QueryCellInfo event. Querying the style info of a cell using grid[rowindex, colindex].text in the QueryCellInfo will fire the event again and will lead to a recursive call. You should adopt a logic not to Querycellinfo inside this event.
One way of acheving this would be to set a flag in the ClosedCellDropDown when a particular value is selected and assign the datasource only when the flag is set. In the attached sample choose the value "One" in cell 2,2 to change the DataSource for a ComboBox in the Cell 3,3.
C#
private void gridModel_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if(e.ColIndex == 3 && e.RowIndex == 3)
{
e.Style.CellType = "ComboBox";
e.Style.ChoiceList = null;
}
if(e.ColIndex == 3 && e.RowIndex == 3 && flag)
{
StringCollection items = new StringCollection();
items.Add("A");
items.Add("B");
items.Add("C");
items.Add("D");
items.Add("E");
e.Style.ChoiceList = items;
}
}
private bool flag = false;
private void gridDataBoundGrid1_CurrentCellCloseDropDown(object sender,
Syncfusion.Windows.Forms.PopupClosedEventArgs e)
{
GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
if(cc.ColIndex == 2 && cc.RowIndex == 2)
{
flag = false;
GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
if (cr.ControlText == "One")
{
flag = true;
}
else
flag = false;
}
}
VB
Private Function GetTable() As DataTable
Dim dt As DataTable = New DataTable("MyTable")
Dim nCols As Integer = 4
Dim nRows As Integer = 10
Dim i As Integer = 0
Do While i < nCols
dt.Columns.Add(New DataColumn(String.Format("Col{0}", i)))
i += 1
Loop
i = 0
Do While i < nRows
Dim dr As DataRow = dt.NewRow()
Dim j As Integer = 0
Do While j < nCols
dr(j) = String.Format("row{0} col{1}", i, j)
j += 1
Loop
dt.Rows.Add(dr)
i += 1
Loop
Return dt
End Function
Private flag As Boolean = False
Private Sub gridDataBoundGrid1_CurrentCellCloseDropDown(ByVal sender As Object, ByVal e As
Syncfusion.Windows.Forms.PopupClosedEventArgs) Handles gridDataBoundGrid1.CurrentCellCloseDropDown
Dim cc As GridCurrentCell = Me.gridDataBoundGrid1.CurrentCell
If cc.ColIndex = 2 AndAlso cc.RowIndex = 2 Then
flag = False
Dim cr As GridComboBoxCellRenderer = CType(IIf(TypeOf cc.Renderer Is GridComboBoxCellRenderer, cc.Renderer,
Nothing), GridComboBoxCellRenderer)
If cr.ControlText = "One" Then
flag = True
Else
flag = False
End If
End If
End Sub
Here is the link with both CS and VB samples: http://help.syncfusion.com/support/samples/kb/grid.windows/GDBGCombo/Combo.zip