Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Data binding for Multiple Controls

using System.Data.SqlClient; using System.Data; using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;   public class MultipleControls : System.Windows.Forms.Form   {     private System.Windows.Forms.DataGrid dataGrid1;     private System.Windows.Forms.ComboBox cboCustomerID;     private System.Windows.Forms.Button button1;     private System.Windows.Forms.Label label1;     private System.Windows.Forms.TextBox txtCustomerID;     private System.Windows.Forms.Label label2;     private System.Windows.Forms.Label label3;     private System.Windows.Forms.TextBox txtContactName;     public MultipleControls()     {       this.dataGrid1 = new System.Windows.Forms.DataGrid();       this.cboCustomerID = new System.Windows.Forms.ComboBox();       this.button1 = new System.Windows.Forms.Button();       this.label1 = new System.Windows.Forms.Label();       this.txtCustomerID = new System.Windows.Forms.TextBox();       this.label2 = new System.Windows.Forms.Label();       this.label3 = new System.Windows.Forms.Label();       this.txtContactName = new System.Windows.Forms.TextBox();       ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();       this.SuspendLayout();       //        this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)          | System.Windows.Forms.AnchorStyles.Left)          | System.Windows.Forms.AnchorStyles.Right);       this.dataGrid1.CaptionVisible = false;       this.dataGrid1.DataMember = "";       this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;       this.dataGrid1.Location = new System.Drawing.Point(12, 112);       this.dataGrid1.Name = "dataGrid1";       this.dataGrid1.Size = new System.Drawing.Size(436, 168);       this.dataGrid1.TabIndex = 1;       //        // cboCustomerID       //        this.cboCustomerID.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;       this.cboCustomerID.Location = new System.Drawing.Point(120, 8);       this.cboCustomerID.Name = "cboCustomerID";       this.cboCustomerID.Size = new System.Drawing.Size(260, 21);       this.cboCustomerID.TabIndex = 2;       //        // button1       //        this.button1.Location = new System.Drawing.Point(340, 80);       this.button1.Name = "button1";       this.button1.Size = new System.Drawing.Size(104, 24);       this.button1.TabIndex = 3;       this.button1.Text = "Test";       this.button1.Visible = false;       this.button1.Click += new System.EventHandler(this.button1_Click);       //        // label1       //        this.label1.Location = new System.Drawing.Point(12, 12);       this.label1.Name = "label1";       this.label1.Size = new System.Drawing.Size(96, 20);       this.label1.TabIndex = 4;       this.label1.Text = "Select By ID:";       //        // txtCustomerID       //        this.txtCustomerID.Location = new System.Drawing.Point(120, 44);       this.txtCustomerID.Name = "txtCustomerID";       this.txtCustomerID.Size = new System.Drawing.Size(188, 21);       this.txtCustomerID.TabIndex = 5;       this.txtCustomerID.Text = "";       //        // label2       //        this.label2.Location = new System.Drawing.Point(12, 48);       this.label2.Name = "label2";       this.label2.Size = new System.Drawing.Size(80, 16);       this.label2.TabIndex = 6;       this.label2.Text = "Customter ID:";       //        // label3       //        this.label3.Location = new System.Drawing.Point(12, 72);       this.label3.Name = "label3";       this.label3.Size = new System.Drawing.Size(104, 16);       this.label3.TabIndex = 7;       this.label3.Text = "Contact Name:";       //        // txtContactName       //        this.txtContactName.Location = new System.Drawing.Point(120, 68);       this.txtContactName.Name = "txtContactName";       this.txtContactName.Size = new System.Drawing.Size(188, 21);       this.txtContactName.TabIndex = 8;       this.txtContactName.Text = "";       //        // MultipleControls       //        this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);       this.ClientSize = new System.Drawing.Size(456, 294);       this.Controls.AddRange(new System.Windows.Forms.Control[] {                                       this.txtContactName,                                       this.label3,                                       this.label2,                                       this.txtCustomerID,                                       this.label1,                                       this.button1,                                       this.cboCustomerID,                                       this.dataGrid1});       this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));       this.Load += new System.EventHandler(this.MultipleControls_Load);       ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();       this.ResumeLayout(false);     }     private void MultipleControls_Load(object sender, System.EventArgs e)     {       string connectionString = @"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI";       string SQL = "SELECT * FROM Customers";       SqlConnection con = new SqlConnection(connectionString);       SqlCommand com = new SqlCommand(SQL, con);       SqlDataAdapter adapter = new SqlDataAdapter(com);       DataSet ds = new DataSet("Northwind");             con.Open();       adapter.Fill(ds, "Customers");       com.CommandText = "SELECT * FROM Products";       adapter.Fill(ds, "Products");       com.CommandText = "SELECT * FROM Suppliers";       adapter.Fill(ds, "Suppliers");       con.Close();       dataGrid1.DataSource = ds.Tables["Customers"].DefaultView;       cboCustomerID.DataSource = ds.Tables["Customers"].DefaultView;       cboCustomerID.DisplayMember = "CustomerID";       txtCustomerID.DataBindings.Add("Text", ds.Tables["Customers"].DefaultView,"CustomerID");       txtContactName.DataBindings.Add("Text", ds.Tables["Customers"].DefaultView,"ContactName");     }     private void button1_Click(object sender, System.EventArgs e)     {       BindingContext binding = this.BindingContext;       BindingManagerBase currency = binding[cboCustomerID.DataSource];              DataRowView drView = (DataRowView)currency.Current;       MessageBox.Show(drView["CustomerID"].ToString());     }     [STAThread]     static void Main()      {       Application.Run(new MultipleControls());     }   }