How to Assign Different ComboBox Values in Each Row of a WinForms Datagrid
When working with FlexGrid controls, there can be scenarios where we need to change the dropdown items in a cell depending on the selection made in the previous cell, i.e., to have a cascading effect.
This blog post will demonstrate how to populate the cells of a dropdown column with a dynamic set of values in the following situations:
- Implementing Cascading Effect in Data Bounded FlexGrid
- Implementing Cascading Effect in Unbounded FlexGrid
Ready to Try it Out? Download ComponentOne Today!
Implementing Cascading Effect in Data Bounded FlexGrid
To implement the cascading effect in FlexGrid in bound mode, we will be binding it to the two tiers of the IList data source. As you can see in the GIF below, we are getting a new set of provinces in the dropdown of the second column according to the value in the respective cell of the first column.
For this, let’s first create a custom class named CountryDetails with the property named StateOptions of type List that we will display accordingly in the State cells based on the Country value cell.
Create a list of objects of CountryDetails class with some default values and set this collection as the data source of the FlexGrid control.
Across all the rows, you'll find all of the CountryDetails properties filled in (Country, State, Currency, Population). To populate the dropdown in the State column, we will be focused on the StateOptions property of CountryDetails, as said before.
We've prepopulated the cells in the State column with some default values. Still, we've also provided functionality for the user to change the State values by making the Combobox control the Editor of the current State cell and binding the StateOptions list for that row to the Combobox's DataSource.
For this, we are handling the BeforeEdit event of FlexGrid to set the cell editor and its data source.
This way, each State row will display the values concurrent with the corresponding country value.
Implementing Cascading Effect in Unbounded FlexGrid
In some cases, the grid we're dealing with may not be dependent on any data source, yet we still want to have this cascading effect. The GIF below shows how the value in the Job column dropdown menu adapts to the one chosen in the Region column.
Let’s divide this implementation into the following steps:
- Creation of dropdown data
- Assign the dropdown in the Job Column based on Region selected
Creation of Dropdown Data
Create the data enumeration for storing the Region and Job values.
Create a Hashtable and ListDictionary to define the jobs in each region and to set them as the DataMap for Region and Job column, respectively.
Assign the dropdown in Job Column based on Region Selected
By default, the Job column cells are non-editable which will enable when selecting the region for specific row and to populate the values depending up on the selected region, we will handle the SetupEditor event of FlexGrid and use the ComboBox as the Editor of the Job column with required job values.
We hope that this demonstration of implementing the cascading effect in the FlexGrid cells while working in bound and unbound modes will make both modes interesting to you.
Download the sample for this blog.
Please try it out and leave your feedback or questions in the comments section. Happy Coding!
Learn more about FlexGrid.
Ready to Try it Out? Download ComponentOne Today!