Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Multi-column Combo

0.00/5 (No votes)
6 Apr 2005 1  
A multi-column combo in VB.NET.

Introduction

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 (ComboBox and ListView) and a few lines of coding.

The properties and methods in this User Control are as follows:

Properties

  • ColumnSize

    Used to specify the column size of the combo.

  • ColumnTitle

    To specify the column title of the combo.

  • DataFieldList

    Used to store the data of a selected item.

    Example: in the demo combo, there are two columns, EmployeeCode and 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.

  • DataFieldToDisplay

    The item of the column to display in the control, after selection.

  • DataSource

    If we want to load the data from the database, we can make use of this property.

    Example:

    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)
    Appcn.Open()
    Da = New System.Data.OleDb.OleDbDataAdapter("Select * From Employee", Appcn)
    Ds = New DataSet("Combo")
    Da.Fill(Ds, "Combo")
    MulCol.DataSource = Ds.Tables("Combo")
  • ItemsToDisplay

    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:

    RAC001,RACHID

    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.

  • RowsToDisplay

    This is to specify how many rows are to be displayed in the drop down.

  • Text

    In this property, the selected item will be stores.

  • Value

    This property will store the value of �DataFieldList�.

Method

  • AddItem

    This method is used to add items in the combo at run time. The columns have to be separated by comma (,). E.g.:

    MulCol.AddItem("RAC001,RACHID")

Property Page

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.

MulCol.LoadData() '---- This is the code to load the combo

Assume that the UserControl name is �MulCol�.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here