ComponentOne MultiSelect for WPF
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 ItemsSource and DisplayMemberPath properties for the MultiSelect control.
      Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
          mselect.ItemsSource = GetDataSource("Select * from Employees ").AsDataView()
          mselect.BindingInfo.DisplayMemberPath = "FirstName"
      End Sub
      
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          mselect.ItemsSource = GetDataSource("Select * from Employees ").AsDataView();
          mselect.BindingInfo.DisplayMemberPath = "FirstName";
      }