Professional UI Solutions
Site Map   /  Register
 
 
 

Data Bound Grid

How to populate a combo box in a datagrid cell with data from a data source?

In the Elegant Grid, every column and cell class has the following properties:

  • MappingDataSource
  • MappingValueDataMember
  • MappingDisplayDataMember

Using these properties, you can populate the combo box in a particular cell or all combo boxes in a column in one of the following ways depending on the data source and if you want to use the column mapping.

1. If you have a one-dimensional array as the data source, set the MappingDataSource property of a cell to a reference to the array and MappingValueDataMember to [-1]:

ComboBoxCell myCell;

// ...

string[] myStrings = new string[] { "One", "Two", "Three" };
myCell.MappingDataSource = myStrings;
myCell.MappingValueDataMember = "[-1]";

If you want to populate all combo boxes in a column:

Column myColumn;

// ...

Array values = Enum.GetValues(typeof(MyEnum));

myColumn.MappingDataSource = values;
myColumn.MappingValueDataMember = "[-1]";

2. If you have tabular data (e.g., an instance of DataTable, a two-dimensional array, etc) and you want to fill the combo box with the values from a column in this table, you can use the following code for a cell:

ComboBoxCell myCell;

// ...

DataTable table = new DataTable();

// ...

myCell.MappingDataSource = table;
myCell.MappingValueDataMember = "CustomerName"; // Column name

If you have an array and want to populate all combo boxes in a column:

Column myColumn;

// ...

string[,] array = new string[3, 10];

// ...

myColumn.MappingDataSource = array;
myColumn.MappingValueDataMember = "[1]"; // Column array index.

3. If you have tabular data like in previous case, but you want to use the column mapping, you can use the following code for a cell:

ComboBoxCell myCell;

// ...

DataTable table = new DataTable();

// ...

myCell.MappingDataSource = table;
myCell.MappingValueDataMember = "CustomerId";
myCell.MappingDisplayDataMember = "CustomerName";

If you want to populate all combo boxes in a column:

Column myColumn;

// ...

DataTable table = new DataTable();

// ...

myColumn.MappingDataSource = table;
myColumn.MappingValueDataMember = "CustomerId";
myColumn.MappingDisplayDataMember = "CustomerName";

How to populate the data grid with columns for my object collection in VS Designer?

If you are using a simple collection of objects as the data source for your grid, you cannot simply click a button in the designer and populate the grid with columns as in case with ADO.NET data sources. So follow these simple steps to achieve the same:

  • Select the grid in the Visual Studio Designer.
  • In the Properties pane, click the ... button in the Columns property so that Columns Collection Editor appears.
  • Click the Add button to add as many columns as you need.
  • Each grid column should be linked with a property of the class for the objects in your collection. You can set such a link by specifying the class's property name in the DataPropertyName property of the column. For example, if you have an Employee class with the properties Name and Age and you want to display these properties in a grid, create two columns and set Name and Age in the DataPropertyName properties of these columns.
  • You can also adjust other column parameters like width, caption, cell type, cell styles, etc.
  • Click OK and run you application.

How to add/remove a row to/from the data bound grid?

You can add/remove a row to/from the data bound grid by adding/removing the corresponding data object to/from your data source list. This mechanism works only if you are using an ADO.NET container as the data source or if your custom data source collection implements the standard IBindingList interface.

When working with the bound grid, I encountered that some row and cell objects are invalid. What's wrong?

The rows and cells in the bound grid are not long living, persistent objects. In fact, a particular cell exists only when it is visible. This approach makes it possible to dramatically reduce grid creation time and memory usage overhead.

For example, if you have a reference to a cell instance you got earlier, when handling a grid event and try to access methods and properties of this cell, you may have a problem when the state of this cell instance is invalid or the object represents some other cell.

Instead you can use persistent entities like data source items, row indices, columns and etc. The bound grid control gives you a full set of interfaces to follow this approach.

How to set up grouping with the Windows Forms Designer?

Make sure that the GroupByBoxVisible property is set to true in the Properties pane of Visual Studio (the Group By Box should be visible at the top of the grid control). Drag a header of the column you want to sort the contents by and drop it on the Group By Box. As a result, the data relating to grouping will be serialized to the Form Designer generated code. Set the GroupByBoxVisible property back to false if you do not want the Group By Box to be visible at run time. Now if you run your application, the rows will be grouped as you arranged them at design time.


If you have any questions, please visit our forum or contact our technical support team at support@prof-uis.com.