Introduction
After searching for a MultiColumn ComboBox with configurable view columns, configurable display & value members, I decided to create my own control.
The Multi-Column ComboBox is a combination of TextBox, Button & DataGridView control. Since I had a need to create a control like a ComboBox and with search functionality, I decided to use DataGridView & it's filter criteria for fast search functionality.
One of the harder things to figure out was what container to show on key press of the combo control. I used ToolStripDropDown as a host control for DataGridView to show the ComboBox popup on key press.
Common Properties:
- Show Header: Set column header visibility.
- Grid Lines: Set grid lines as None, Vertical, Horizontal or Both.
- DropDown Height: Configurable dropdown height. It will show scrollbar if more items present.
- DataSource: DataSource will be a DataTable.
- SourceDataString: Mapping Properties which will be shown as Columns.
- SourceDataHeader: Column Headers which will be shown if Show Header property is true.
- ColumnWidth: Width for each column.
- Display Column No: Column number which will be work as a DisplayMember.
- Value Column No: Column number which will be work as a ValueMember.
Usage
The Control usage is very simple & straight forward. Set the following properties to configure the control:
private void InitializeComboBox()
{
multiColumComboBox1.Clear();
multiColumComboBox1.SourceDataString = new string[3] { "ID", "Name", "Code" };
multiColumComboBox1.ColumnWidth = new string[3] { "0", "200", "50" };
multiColumComboBox1.DataSource = GetDataSource();
}
private DataTable GetDataSource()
{
DataTable dtSource = new DataTable();
DataColumn dtColID = new DataColumn("ID");
dtColID.DataType = System.Type.GetType("System.String");
dtSource.Columns.Add(dtColID);
DataColumn dtColName = new DataColumn("Name");
dtColName.DataType = System.Type.GetType("System.String");
dtSource.Columns.Add(dtColName);
DataColumn dtColCode = new DataColumn("Code");
dtColCode.DataType = System.Type.GetType("System.String");
dtSource.Columns.Add(dtColCode);
//Add rows
DataRow row = dtSource.NewRow();
row[dtColID] = "1";
row[dtColName] = "Vijay";
row[dtColCode] = "1001";
dtSource.Rows.Add(row);
//Add rows
row = dtSource.NewRow();
row[dtColID] = "2";
row[dtColName] = "Ajay";
row[dtColCode] = "1002";
dtSource.Rows.Add(row);
. . . . .
. . . . .
return dtSource;
}