ComponentOne MultiSelect for WinForms
Data Binding / Bind MultiSelect to a Data Source
In This Topic
    Bind MultiSelect to a Data Source
    In This Topic

    To bind MultiSelect to a data source, follow these steps:

    1. Create a connection string and fetch data from a database to a data set.
      Private Shared Function GetConnectionString() As String
          Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Common\C1NWind.mdb;"
          Return String.Format(conn)
      End Function
      
      Private Function GetDataSource(ByVal connectionString As String) As DataTable
          'set up connection string
          Dim conn As String = GetConnectionString()
      
          'set up SQL statement
          Dim rs As String = connectionString
      
          'retrieve data into DataSet
          Dim da As OleDbDataAdapter = New OleDbDataAdapter(rs, conn)
          Dim ds As DataSet = New DataSet()
          da.Fill(ds)
      
          'return data table
          Return ds.Tables(0)
      
      End Function
      
      static string GetConnectionString()
      {
            string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Common\C1NWind.mdb;";
            return string.Format(conn);
      }
      DataTable GetDataSource(string connectionString)
      {
            // set up connection string
            string conn = GetConnectionString();
      
            // set up SQL statement
            string rs = connectionString;
      
           // retrieve data into DataSet
           OleDbDataAdapter da = new OleDbDataAdapter(rs, conn);
           DataSet ds = new DataSet();
           da.Fill(ds);
      
           // return data table
           return ds.Tables[0];
      }
      
    2. Set the DataSource and DisplayMemberPath properties of the MultiSelect control.
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          C1MultiSelect1.BindingInfo.DataSource = GetDataSource("Select * from Employees ")
          C1MultiSelect1.BindingInfo.DataMember = "EmployeeID"
          C1MultiSelect1.BindingInfo.DisplayMemberPath = "FirstName"
      End Sub
      
      private void Form1_Load(object sender, EventArgs e)
      {
         c1MultiSelect1.BindingInfo.DataSource = GetDataSource("Select * from Employees ");
         c1MultiSelect1.BindingInfo.DisplayMemberPath = "FirstName";
      }