We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Article ID: Published Date: Last Revised Date: Platform: Control:
477 04/30/2011 07/10/2014 WinForms GridDataBoundGrid(Classic)
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Please sign in to access our KB

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon