The ComboBoxEditor control can be used to view the content of cells which have an ID/Value mapping or to display custom unbound data in a single or multi-column combobox.
The ComboBoxEditor control is a multi-column combobox where each column is represented by a ColumnInfo object, and each item (line) is represented by a ComboBoxItem object. Columns are accessible via the Columns collection while items are accessible via the Items collection.
The suggested method of adding items to a combobox is to first add the desired columns (specifying various characteristics) and then to add the items. The datatype of the values in the items that are added must match the datatype of the corresponding columns, and each item must contain the same number of values as there are columns in the combobox. If the number of values is greater than or less than the number of columns, or if their datatype is different than the corresponding column's, an exception will be thrown.
Items can either be added either directly as ComboBoxItem's, or as comma separated values (as demonstrated below).
See example
VB.NET |
Copy Code |
---|---|
Dim combo As New ComboBoxEditor() combo.Columns.Add( New ColumnInfo( "Country", GetType( String ) ) ) combo.Columns.Add( New ColumnInfo( "Population", GetType( Integer ) ) ) combo.Columns.Add( New ColumnInfo( "City", GetType( String ) ) ) combo.Columns.Add( New ColumnInfo( "AverageTemp", GetType( String ) ) ) combo.Items.Add( "Canada", 31500000, "Ottawa", "12.2 ºc" ) combo.Items.Add( "Switzerland", 7300000, "Bern", "23.3 ºc" ) combo.Items.Add( "France", 59500000, "Paris", "27.3 ºc" ) combo.Items.Add( "USA", 278000000, "Washington", "14.1 ºc" ) combo.Items.Add( "UK", 59700000, "London", "23.7 ºc" ) combo.Items.Add( "Belgium", 10300000, "Brussels", "21.8 ºc" ) combo.Items.Add( "Italy", 57700000, "Rome", "29.6 ºc" ) combo.Items.Add( "Spain", 40000000, "Madrid", "31.8 ºc" ) combo.Items.Add( "Germany", 83000000, "Berlin", "25.1 ºc" ) combo.Items.Add( "Japan", 126800000, "Tokyo", "17.2 ºc" ) GridControl1.Columns( "Country_Info" ).CellEditorManager = combo |
C# |
Copy Code |
---|---|
WinComboBox combo = new WinComboBox(); combo.Columns.Add( new ColumnInfo( "Country", typeof( string ) ) ); combo.Columns.Add( new ColumnInfo( "Population", typeof( int ) ) ); combo.Columns.Add( new ColumnInfo( "City", typeof( string ) ) ); combo.Columns.Add( new ColumnInfo( "AverageTemp", typeof( string ) ) ); combo.Items.Add( "Canada", 31500000, "Ottawa", "12.2 ºc" ); combo.Items.Add( "Switzerland", 7300000, "Bern", "23.3 ºc" ); combo.Items.Add( "France", 59500000, "Paris", "27.3 ºc" ); combo.Items.Add( "USA", 278000000, "Washington", "14.1 ºc" ); combo.Items.Add( "UK", 59700000, "London", "23.7 ºc" ); combo.Items.Add( "Belgium", 10300000, "Brussels", "21.8 ºc" ); combo.Items.Add( "Italy", 57700000, "Rome", "29.6 ºc" ); combo.Items.Add( "Spain", 40000000, "Madrid", "31.8 ºc" ); combo.Items.Add( "Germany", 83000000, "Berlin", "25.1 ºc" ); combo.Items.Add( "Japan", 126800000, "Tokyo", "17.2 ºc" ); gridControl1.Columns[ "Country_Info" ].CellEditorManager = combo; |
The values of items that are added to the combobox using the suggested method (demonstrated above) can then be accessed using either their index or corresponding column name.
Although it is suggested that columns be added to the combobox prior to adding items (lines), it is not an obligation. Items can be added without first adding columns; however, the following rules apply:
The first ComboBoxItem that is added will determine the number of columns that are contained in the combobox. Items that are subsequently added must contain the same number of values. No more, no less.
Default names will be assigned to each ColumnInfo (column) that is created by the first item that is added ( "Column1", "Column2", etc.). These names cannot be modified. In the case where the combobox is bound to a data source, the column names will be the names of the fields in the data source. In the case where the data source is a jagged array, the column names will be "0", "1", "2", etc.
All the columns will have an object datatype.
The following example demonstrates how to add items to the ComboBoxEditor without previously adding columns (ColumnInfo objects). Unlike the previous examples, items will be added as ComboBoxItems and not as comma separated values.
See example
VB.NET |
Copy Code |
---|---|
Dim combo As New ComboBoxEditor() combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Canada", 31500000, "Ottawa", "12.2 ºc" } ) ) combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Switzerland", 7300000, "Bern", "23.3 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "France", 59500000, "Paris", "27.3 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "USA", 278000000, "Washington", "14.1 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "UK", 59700000, "London", "23.7 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Belgium", 10300000, "Brussels", "21.8 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Italy", 57700000, "Rome", "29.6 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Spain", 40000000, "Madrid", "31.8 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Germany", 83000000, "Berlin", "25.1 ºc" } ) )combo.Items.Add( New ComboBoxItem( New Object( 3 ){ "Japan", 126800000, "Tokyo", "17.2 ºc" } ) ) GridControl1.Columns( "Country_Info" ).CellEditorManager = combo |
C# |
Copy Code |
---|---|
ComboBoxEditor combo = new ComboBoxEditor(); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Canada", 31500000, "Ottawa", "12.2 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Switzerland", 7300000, "Bern", "23.3 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "France", 59500000, "Paris", "27.3 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "USA", 278000000, "Washington", "14.1 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "UK", 59700000, "London", "23.7 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Belgium", 10300000, "Brussels", "21.8 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Italy", 57700000, "Rome", "29.6 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Spain", 40000000, "Madrid", "31.8 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Germany", 83000000, "Berlin", "25.1 ºc" } ) ); combo.Items.Add( new ComboBoxItem( new object[ 4 ]{ "Japan", 126800000, "Tokyo", "17.2 ºc" } ) ); gridControl1.Columns[ "Country_Info" ].CellEditorManager = combo; |
To bind the ComboBoxViewer to a datasource, the DataSource and DataMember properties must be set (or the SetDataBinding method called), the "ID" column specified through the ValueMember property, and the "Value/Name" column specified through the DisplayFormat property. For more information on how to bind the ComboBoxEditor to a datasource, refer to the Data binding topic.
The ComboBoxEditor is never used as a default CellEditorManager unless specified otherwise by either setting the CellEditorManager property of a specific column or cell, or by mapping it to a datatype via the GridControl's CellEditorManagerMapping property.
See example
VB.NET |
Copy Code |
---|---|
Dim editor As New ComboBoxEditor( northwindDataSet, "Products", "ProductID", "%ProductName%" ) GridControl1.Columns( "ProductID" ).CellEditorManager = editor |
C# |
Copy Code |
---|---|
ComboBoxEditor editor = new ComboBoxEditor( northwindDataSet, "Products", "ProductID", "%ProductName%" ); gridControl1.Columns[ "ProductID" ].CellEditorManager = editor ; |
The DisplayFormat property determines the format with which the selected item is displayed. By default, if no display format is specified, the values of each column in the selected item—separated by a comma (,)—will be displayed. For more information on the DisplayFormat property, refer to the Display format topic.
To display images in the ComboBoxEditor, the editor's ImageMember property must be set to the name of the field in the datasource from which to retrieve the value. The position, alignment, padding, size, and style of the images can be defined by the ImagePosition, ImageAlignment, ImagePadding, ImageSize, and ImageStyle properties respectively.
See example
VB.NET |
Copy Code |
---|---|
Dim editor As New ComboBoxEditor( dataSet11, "Employees", "EmployeeID", "Photo", _ Xceed.Editors.ImagePosition.Left, _ New Size( 32, 32 ), "%FirstName% %LastName%" ) GridControl1.Columns( "EmployeeID" ).CellEditorManager = editor |
C# |
Copy Code |
---|---|
ComboBoxEditor editor = new ComboBoxEditor( dataSet11, "Employees", "EmployeeID", "Photo", Xceed.Editors.ImagePosition.Left,new Size( 32, 32 ), "%FirstName% %LastName%" ); grid.Columns[ "EmployeeID" ].CellEditorManager = editor; |
The underlying control wrapped by the ComboBoxEditor control is the WinComboBox control and is accessible through the TemplateControl property. All settings relating to the underlying control must be accessed via the TemplateControl property. Note that if a cloned instance of the TemplateControl is used to edit (refer to the CreateControlMode property and CreateControl method), the actual control that is used will be a clone of the TemplateControl and not the TemplateControl itself. In this case, the ActivatingControl event can be used to modify the cloned instances.
More information regarding the WinComboBox control can be found in the WinComboBox control topic.