|
|
About Combobox in DataGrid (.NET framework)
| |
.NET datagrid is a graphical user interface component that presents a tabular view of data. .NET datagrid supports the following common interface features:
|
C#
// Set column styles
// Set datagrid ColumnStyle for Car field
DataGridTextBoxColumn colCar = new DataGridTextBoxColumn();
colCar.MappingName = "Car";
colCar.HeaderText = "Car Name";
colCar.Width = 130;
colCar.NullText = String.Empty;
colCar.ReadOnly = true;
TblStyle.GridColumnStyles.Add(colCar);
// Set datagrid ComboBox ColumnStyle for PubID field
DataTable tblCompanies = ds.Tables["Companies"];
TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblCompanies, 1, 0, true, false, true));
// Datagrid ComboBox DisplayMember field has order number 1. Name of this column is "Name"
// Datagrid ComboBox ValueMember field has order number 0. Name of this column is "PubID"
TblStyle.GridColumnStyles[1].MappingName = "PubID";
TblStyle.GridColumnStyles[1].HeaderText = "Company ID";
TblStyle.GridColumnStyles[1].Width = 200;
TblStyle.GridColumnStyles[1].NullText = String.Empty;
// Set datagrid combobox ColumnStyle for State field
DataTable tblStates = ds.Tables["States"];
TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblStates, 0, 0, true, false, false));
// Datagrid ComboBox DisplayMember field has order number 0. Name of this column is "State"
// Datagrid ComboBox ValueMember field has order number 0. It is the same column like for DisplayMember
TblStyle.GridColumnStyles[2].MappingName = "State";
TblStyle.GridColumnStyles[2].HeaderText = "State";
TblStyle.GridColumnStyles[2].Width = 45;
TblStyle.GridColumnStyles[2].NullText = String.Empty;
// Set datagrid XP Style Button ColumnStyle for City field
TblStyle.GridColumnStyles.Add(new DataGridXPButtonColumn());
// Also you may set datagrid Button ColumnStyle for City
// field without XP Button Style as the following:
// .Add(New DataGridButtonColumn())
TblStyle.GridColumnStyles[3].MappingName = "City";
TblStyle.GridColumnStyles[3].HeaderText = "City";
TblStyle.GridColumnStyles[3].Width = 60;
TblStyle.GridColumnStyles[3].NullText = String.Empty;
// Set datagrid Memo ColumnStyle for Comments field
TblStyle.GridColumnStyles.Add(new DataGridMemoColumn("Car description"));
TblStyle.GridColumnStyles[4].MappingName = "Comments";
TblStyle.GridColumnStyles[4].HeaderText = "Comments";
TblStyle.GridColumnStyles[4].Width = 60;
|
There are several frequently asked questions regarding datagrid combobox column implementation listed below.
|
How to change the background color for DataGridComboBoxColumn columns in a datagrid? |
The DataGridComboBoxColumn's combo object has the same properties as the .NET ComboBox control.
The following example sets the BackColor property so that will display red background.
This code assumes that an instance of a DataGridComboBoxColumn grid column style has been created on your custom DataGrid.
|
How to setup the combobox that filters values from a related child table? |
The DataGridComboBoxColumn column style control gives an ability to get a nesting combo box that filters values from a related child table. In a dictionary grid, you can view and edit related data in a child table.
In our sample database we have "Corps" Master table that has a one-to-many relationship with a "Cars" Child table; so for each row of the "Corps" table in Dictionary grid, we can view and edit the related rows of the "Cars" child table in a dictionary grid. These two tables are related each other by fields key: "ID + Corp".
For this case we have to define two string arrays with key fields' names MasterFields and ChildFields. To get the filtering we must identify Master and Child tables' key fields. These fields names in both arrays must have the same order sequence.
|
How to define columns for a Dictionary datagrid? |
DataGridComboBoxColumn ActivateDictionary event runs when the datagrid ComboBoxColumn Style's
nesting Dictionary datagrid is activated (the additional dropdown button is clicked).
We use the procedure to specify what dictionary columns will be shown.
|
C#
// Define "Models" Dictionary Grid Column
ModelColumn = (DataGridComboBoxColumn) DataGrid1.TableStyles[0].GridColumnStyles[3];
// ModelColumn object handles the
// Rustemsoft.DataGridColumns.DataGridComboBoxColumn.ActivateDictionary event
ModelColumn.ActivateDictionary +=
new Rustemsoft.DataGridColumns.DataGridComboBoxColumn.ActivateDictionaryEventHandler(this.ModelColumn_ActivateDictionary);
.............
private void ModelColumn_ActivateDictionary(DataGridComboBoxColumn sender)
{
DataGridTableStyle TableStyle = new DataGridTableStyle();
GridColumnStylesCollection DictionrGridColumns = TableStyle.GridColumnStyles;
// Define columns
DictionrGridColumns.Add(new DataGridTextBoxColumn());
DictionrGridColumns[0].MappingName = "ModelID";
DictionrGridColumns[0].HeaderText = "Model ID";
DictionrGridColumns[0].Width = 40;
DictionrGridColumns[0].NullText = String.Empty;
DictionrGridColumns.Add(new DataGridTextBoxColumn());
DictionrGridColumns[1].MappingName = "Model";
DictionrGridColumns[1].HeaderText = "Model";
DictionrGridColumns[1].Width = 60;
DictionrGridColumns[1].NullText = String.Empty;
DataTable tblBodyStyle = ds.Tables["BodyStyle"];
// One more DataGridComboBoxColumn object is enclosed into the datagrid
DictionrGridColumns.Add(new DataGridComboBoxColumn(ref tblBodyStyle, 0, 0, true, false, true, Rustemsoft.DataGridColumns.DataGridComboBoxColumn.DisplayModes.ShowDisplayMember,0));
DictionrGridColumns[2].MappingName = "Body Style";
DictionrGridColumns[2].HeaderText = "Body Style";
DictionrGridColumns[2].Width = 120;
DictionrGridColumns[2].NullText = String.Empty;
DictionrGridColumns.Add(new DataGridNumericColumn(true, true, DateTime.Now.Year, 1955, 2020, 4, null));
DictionrGridColumns[3].MappingName = "Year";
DictionrGridColumns[3].HeaderText = "Year";
DictionrGridColumns[3].Width = 50;
DictionrGridColumns[3].NullText = String.Empty;
// Define Dictionary Grid Columns Styles
ModelColumn.combo.DictionaryGridColumns = DictionrGridColumns;
// Define Dictionary Grid Height
ModelColumn.combo.DictionaryGridHeight = 250;
// Define Dictionary Grid Width
ModelColumn.combo.DictionaryGridWidth = 350;
}
|
|
|