This is a simple multi-column combo having the facility to load data from a table, and sort the data in the combo by clicking the column header. In this
UserControl, I have used Windows Forms controls (
ListView) and a few lines of coding.
The properties and methods in this User Control are as follows:
Used to specify the column size of the combo.
To specify the column title of the combo.
Used to store the data of a selected item.
Example: in the demo combo, there are two columns,
Name. When we select an item from the combo list, we want to store the employee code in the database and the name has to be displayed as a text in the control. This property is used to keep the employee code.
The item of the column to display in the control, after selection.
If we want to load the data from the database, we can make use of this property.
Dim CnStr As String
Dim Appcn As New System.Data.OleDb.OleDbConnection()
Dim Da As New System.Data.OleDb.OleDbDataAdapter()
Dim Ds As DataSet
CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=D:\Data\Employee.MDB"
Appcn = New System.Data.OleDb.OleDbConnection(CnStr)
Da = New System.Data.OleDb.OleDbDataAdapter("Select * From Employee", Appcn)
Ds = New DataSet("Combo")
MulCol.DataSource = Ds.Tables("Combo")
If we need to add some items to the combo not from the database, we can make use of this property. It’s a design time property. If we need more than one column, we have to separate them by comma (,). Example:
In the above example, ‘
RAC001’ is an employee code and
RACHID is the employee name. So in the combo, it will come as two columns.
This is to specify how many rows are to be displayed in the drop down.
In this property, the selected item will be stores.
This property will store the value of ‘
This method is used to add items in the combo at run time. The columns have to be separated by comma (,). E.g.:
How to use this control
Add this control into the project. Set the properties as mentioned above, in the property sheet. Place the following code in the form load event handler.
Assume that the
UserControl name is ‘
Note: If you are not going to load the data from the table, then don’t forget to add the data in the ‘
ItemsToDisplay’ property. If you need to add more than one column, then separate the first item and the following items by a comma (,).
For more details, you can see the demo application. It’s a self explanatory one. And the user control also is a self explanatory one. And wherever comments are needed, I have placed the required comments too.