Data Binding: ListBox : DataBinding ListBox « ADO.Net « C# / CSharp Tutorial






Data Binding: ListBox
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

public class ListBoxDataBindingDatabase : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.ListBox listBox1;

  private System.ComponentModel.Container components = null;

  public ListBoxDataBindingDatabase()
  {
    InitializeComponent();
  }

  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.listBox1 = new System.Windows.Forms.ListBox();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.Location = new System.Drawing.Point(8, 24);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(192, 232);
    this.dataGrid1.TabIndex = 0;
    // 
    // listBox1
    // 
    this.listBox1.Location = new System.Drawing.Point(216, 32);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(104, 212);
    this.listBox1.TabIndex = 1;
    // 
    // ListBoxDataBindingDatabase
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(480, 277);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.listBox1, this.dataGrid1});
    this.Name = "ListBoxDataBindingDatabase";
    this.Text = "ListBoxDataBindingDatabase";
    this.Load += new System.EventHandler(this.ListBoxDataBindingDatabase_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxDataBindingDatabase());
  }

  private void FillDataGrid()
  {
    string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb";
    OleDbConnection conn = new OleDbConnection(ConnectionString);
    string SQL = "SELECT * FROM Customers";

    conn.Open();

    OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, conn);

    DataSet ds = new DataSet("Customers");
    adapter.Fill(ds, "Customers");

    dataGrid1.DataSource = ds.DefaultViewManager;

    conn.Close();    
  }

  private void FillListBox()
  {
    string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb";
    OleDbConnection conn = new OleDbConnection(ConnectionString);
    string SQL = "SELECT * FROM Customers";

    conn.Open();

    OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, conn);

    DataSet ds = new DataSet("Customers");
    adapter.Fill(ds, "Customers");

    listBox1.DataSource = ds.DefaultViewManager;
    listBox1.DisplayMember = "CustomerID";

    conn.Close();    
  }

  private void ListBoxDataBindingDatabase_Load(object sender, System.EventArgs e)
  {
    FillListBox();
    FillDataGrid();
  }
}








32.54.DataBinding ListBox
32.54.1.Data Binding: ListBoxData Binding: ListBox
32.54.2.Binding ListBox to a Database table
32.54.3.Read data from database table add it to a ListBox
32.54.4.Fill ListBox with data from a DataReader