Csharp/CSharp Tutorial/ADO.Net/DataBinding ListBox — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:19, 26 мая 2010

Binding ListBox to a Database table

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ListBoxBindingTable : System.Windows.Forms.Form
{
  private System.Windows.Forms.ListBox employeeList;
  private System.ruponentModel.Container components = null;
  public ListBoxBindingTable()
  {
    InitializeComponent();
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
    string commandString = "Select ID, FirstName from Employee";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString);
    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet,"Employee");
    DataTable dataTable = dataSet.Tables[0];
    employeeList.DataSource= dataTable;
    employeeList.DisplayMember = "Employee";

  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.employeeList = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    this.employeeList.Location = new System.Drawing.Point(8, 8);
    this.employeeList.Name = "employeeList";
    this.employeeList.Size = new System.Drawing.Size(272, 95);
    this.employeeList.TabIndex = 0;
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 133);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.employeeList});
    this.Name = "ListBoxBindingTable";
    this.Text = "ListBoxBindingTable";
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxBindingTable());
  }
}

Data Binding: ListBox

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
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.ruponentModel.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.ruponentModel.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.ruponentModel.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();
  }
}

Fill ListBox with data from a DataReader

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ListBoxFillFromDataReader : System.Windows.Forms.Form
{
    private System.Data.SqlClient.SqlConnection connection;
    private System.Data.DataSet dataSet;
    private System.Data.SqlClient.SqlCommand command;
    private System.Data.SqlClient.SqlDataAdapter dataAdapter;
    private System.Windows.Forms.ListBox lbBugs;
  private System.ruponentModel.Container components = null;
  public ListBoxFillFromDataReader()
  {
    InitializeComponent();
        string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
    using (connection = new System.Data.SqlClient.SqlConnection(connectionString))
    {
      connection.Open();
      using(dataSet = new System.Data.DataSet())
      {
        dataSet.CaseSensitive=true;
        string commandString = "Select ID, FirstName from Employee";
        command = new System.Data.SqlClient.SqlCommand();
        command.Connection=connection;
        command.rumandText= commandString;
        using (SqlDataReader dataReader = command.ExecuteReader())
        {
          while (dataReader.Read())
          {
            object bugID = dataReader["ID"];
            object description = dataReader["FirstName"];
            lbBugs.Items.Add(bugID.ToString() + ": " +  description.ToString());
          }
        }  
      }    
    }      
    }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
        this.lbBugs = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        this.lbBugs.Location = new System.Drawing.Point(16, 8);
        this.lbBugs.Name = "lbBugs";
        this.lbBugs.Size = new System.Drawing.Size(216, 147);
        this.lbBugs.TabIndex = 0;
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(264, 181);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.lbBugs});
        this.Name = "ListBoxFillFromDataReader";
        this.Text = "ListBoxFillFromDataReader";
        this.ResumeLayout(false);
    }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxFillFromDataReader());
  }
}

Read data from database table add it to a ListBox

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ListBoxTableReadingFill : System.Windows.Forms.Form
{
    private System.Data.SqlClient.SqlConnection myConnection;
    private System.Data.DataSet myDataSet;
    private System.Data.SqlClient.SqlCommand myCommand;
    private System.Data.SqlClient.SqlDataAdapter myDataAdapter;
    private System.Windows.Forms.ListBox employeeList;
  private System.ruponentModel.Container components = null;
  public ListBoxTableReadingFill()
  {
    InitializeComponent();
        string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
        myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
        myConnection.Open();
        myDataSet = new System.Data.DataSet();
        myDataSet.CaseSensitive=true;
        string commandString = "Select ID, FirstName from Employee";

        myCommand = new System.Data.SqlClient.SqlCommand();
        myCommand.Connection=myConnection;
        myCommand.rumandText= commandString;
        myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = myCommand;
        myDataAdapter.TableMappings.Add("Table", "Employee");
        myDataAdapter.Fill(myDataSet);

        DataTable myDataTable = myDataSet.Tables[0];
        
        foreach (DataRow dataRow in myDataTable.Rows)
        {
            employeeList.Items.Add(dataRow["ID"] + ":" + dataRow["FirstName"]  );
        }
    }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
        this.employeeList = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // employeeList
        // 
        this.employeeList.Location = new System.Drawing.Point(16, 8);
        this.employeeList.Name = "employeeList";
        this.employeeList.Size = new System.Drawing.Size(216, 147);
        this.employeeList.TabIndex = 0;
        // 
        // ListBoxTableReadingFill
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(464, 205);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.employeeList});
        this.Name = "ListBoxTableReadingFill";
        this.Text = "ListBoxTableReadingFill";
        this.ResumeLayout(false);
    }
  [STAThread]
  static void Main() 
  {
    Application.Run(new ListBoxTableReadingFill());
  }
}