Data binding for Multiple Controls : DataGrid « ADO.Net « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » DataGrid 
32.52.5.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(12112);
      this.dataGrid1.Name = "dataGrid1";
      this.dataGrid1.Size = new System.Drawing.Size(436168);
      this.dataGrid1.TabIndex = 1;
      // 
      // cboCustomerID
      // 
      this.cboCustomerID.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.cboCustomerID.Location = new System.Drawing.Point(1208);
      this.cboCustomerID.Name = "cboCustomerID";
      this.cboCustomerID.Size = new System.Drawing.Size(26021);
      this.cboCustomerID.TabIndex = 2;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(34080);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(10424);
      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(1212);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(9620);
      this.label1.TabIndex = 4;
      this.label1.Text = "Select By ID:";
      // 
      // txtCustomerID
      // 
      this.txtCustomerID.Location = new System.Drawing.Point(12044);
      this.txtCustomerID.Name = "txtCustomerID";
      this.txtCustomerID.Size = new System.Drawing.Size(18821);
      this.txtCustomerID.TabIndex = 5;
      this.txtCustomerID.Text = "";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(1248);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(8016);
      this.label2.TabIndex = 6;
      this.label2.Text = "Customter ID:";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(1272);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(10416);
      this.label3.TabIndex = 7;
      this.label3.Text = "Contact Name:";
      // 
      // txtContactName
      // 
      this.txtContactName.Location = new System.Drawing.Point(12068);
      this.txtContactName.Name = "txtContactName";
      this.txtContactName.Size = new System.Drawing.Size(18821);
      this.txtContactName.TabIndex = 8;
      this.txtContactName.Text = "";
      // 
      // MultipleControls
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(514);
      this.ClientSize = new System.Drawing.Size(456294);
      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());
    }
  }
32.52.DataGrid
32.52.1.Load data in DataTable to DataGridLoad data in DataTable to DataGrid
32.52.2.Link two DataTable in a DataGridLink two DataTable in a DataGrid
32.52.3.Load Data to DataGridLoad Data to DataGrid
32.52.4.DataGrid View: on data error
32.52.5.Data binding for Multiple Controls
32.52.6.Binding DataSet to DataGrid
32.52.7.Programmatic Data Display
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.