Data Binding 4 : Data Binding « Database ADO.net « C# / C Sharp






Data Binding 4

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace DataBinding
{
    /// <summary>
    /// Summary description for DataBinding4.
    /// </summary>
    public class DataBinding4 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid grdOrders;
    private System.Windows.Forms.DataGrid grdOrderDetails;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.ComboBox cbCust;
    private System.Windows.Forms.TextBox txtPhoneNo;
    private System.Windows.Forms.TextBox txtFaxNo;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.TextBox txtContact;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DataBinding4()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

      SqlConnection cn=new SqlConnection(@"data source=(local);uid=sa;password=;database=northwind");
      
      DataSet ds = new DataSet("CustOrders");

      SqlDataAdapter daCust=new SqlDataAdapter("select * from customers;select * from orders;select * from [order details]",cn);
      daCust.Fill(ds);
      
      ds.Relations.Add("CustOrder",ds.Tables["Table"].Columns["customerid"],ds.Tables["Table1"].Columns["customerid"]);
      ds.Relations.Add("OrderDetail",ds.Tables["Table1"].Columns["orderid"],ds.Tables["Table2"].Columns["orderid"]);

      grdOrders.DataSource=ds;
      grdOrders.DataMember="Table.CustOrder";
      grdOrderDetails.DataSource=ds;
      grdOrderDetails.DataMember="Table.CustOrder.OrderDetail";

      cbCust.DataSource=ds;
      cbCust.DisplayMember="Table.CompanyName";
      txtContact.DataBindings.Add("Text",ds,"Table.ContactName");
      txtPhoneNo.DataBindings.Add("Text",ds,"Table.Phone");
      txtFaxNo.DataBindings.Add("Text",ds,"Table.Fax");
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.grdOrders = new System.Windows.Forms.DataGrid();
      this.grdOrderDetails = new System.Windows.Forms.DataGrid();
      this.cbCust = new System.Windows.Forms.ComboBox();
      this.txtPhoneNo = new System.Windows.Forms.TextBox();
      this.txtFaxNo = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.label3 = new System.Windows.Forms.Label();
      this.label4 = new System.Windows.Forms.Label();
      this.txtContact = new System.Windows.Forms.TextBox();
      ((System.ComponentModel.ISupportInitialize)(this.grdOrders)).BeginInit();
      ((System.ComponentModel.ISupportInitialize)(this.grdOrderDetails)).BeginInit();
      this.SuspendLayout();
      // 
      // grdOrders
      // 
      this.grdOrders.AllowNavigation = false;
      this.grdOrders.DataMember = "";
      this.grdOrders.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.grdOrders.Location = new System.Drawing.Point(40, 176);
      this.grdOrders.Name = "grdOrders";
      this.grdOrders.Size = new System.Drawing.Size(448, 144);
      this.grdOrders.TabIndex = 1;
      // 
      // grdOrderDetails
      // 
      this.grdOrderDetails.DataMember = "";
      this.grdOrderDetails.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.grdOrderDetails.Location = new System.Drawing.Point(40, 328);
      this.grdOrderDetails.Name = "grdOrderDetails";
      this.grdOrderDetails.Size = new System.Drawing.Size(448, 136);
      this.grdOrderDetails.TabIndex = 2;
      // 
      // cbCust
      // 
      this.cbCust.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.cbCust.Location = new System.Drawing.Point(216, 32);
      this.cbCust.Name = "cbCust";
      this.cbCust.Size = new System.Drawing.Size(240, 21);
      this.cbCust.TabIndex = 3;
      // 
      // txtPhoneNo
      // 
      this.txtPhoneNo.Location = new System.Drawing.Point(216, 96);
      this.txtPhoneNo.Name = "txtPhoneNo";
      this.txtPhoneNo.Size = new System.Drawing.Size(240, 20);
      this.txtPhoneNo.TabIndex = 4;
      this.txtPhoneNo.Text = "textBox1";
      // 
      // txtFaxNo
      // 
      this.txtFaxNo.Location = new System.Drawing.Point(216, 128);
      this.txtFaxNo.Name = "txtFaxNo";
      this.txtFaxNo.Size = new System.Drawing.Size(240, 20);
      this.txtFaxNo.TabIndex = 5;
      this.txtFaxNo.Text = "textBox2";
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(80, 32);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(128, 24);
      this.label1.TabIndex = 6;
      this.label1.Text = "Customer Name";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(80, 96);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(128, 24);
      this.label2.TabIndex = 7;
      this.label2.Text = "Phone Number";
      this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(80, 128);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(128, 24);
      this.label3.TabIndex = 8;
      this.label3.Text = "Fax Number";
      this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // label4
      // 
      this.label4.Location = new System.Drawing.Point(80, 64);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(128, 24);
      this.label4.TabIndex = 10;
      this.label4.Text = "Contact Name";
      this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // txtContact
      // 
      this.txtContact.Location = new System.Drawing.Point(216, 64);
      this.txtContact.Name = "txtContact";
      this.txtContact.Size = new System.Drawing.Size(240, 20);
      this.txtContact.TabIndex = 9;
      this.txtContact.Text = "textBox1";
      // 
      // DataBinding4
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(520, 483);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.label4,
                                                                  this.txtContact,
                                                                  this.label3,
                                                                  this.label2,
                                                                  this.label1,
                                                                  this.txtFaxNo,
                                                                  this.txtPhoneNo,
                                                                  this.cbCust,
                                                                  this.grdOrderDetails,
                                                                  this.grdOrders});
      this.Name = "DataBinding4";
      this.Text = "DataBinding4";
      ((System.ComponentModel.ISupportInitialize)(this.grdOrders)).EndInit();
      ((System.ComponentModel.ISupportInitialize)(this.grdOrderDetails)).EndInit();
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new DataBinding4());
        }
    }
}


           
       








Related examples in the same category

1.ADO.NET Binding: Handling ErrorsADO.NET Binding: Handling Errors
2.ADO.NET Binding: Master DetailADO.NET Binding: Master Detail
3.ADO.NET Binding : Multiple Control BindingADO.NET Binding : Multiple Control Binding
4.ADO.NET Binding : UnsynchronizedADO.NET Binding : Unsynchronized
5.Data Binding 3