Simple DataBinding to TextBox

image_pdfimage_print
   



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form {
    public Form1() {
        InitializeComponent();
        DataSet ds = CreateDataSet();
        textBox.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");
    }

    private DataSet CreateDataSet() {
        string customers = "SELECT * FROM Products";
        DataSet ds = new DataSet();

        using (SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["northwind"].ConnectionString)) {
            SqlDataAdapter da = new SqlDataAdapter(customers, con);

            da.Fill(ds, "Products");
        }

        return ds;
    }

    private void InitializeComponent() {
        this.textBox = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(13, 13);
        this.textBox.Name = "textBox";
        this.textBox.Size = new System.Drawing.Size(477, 20);
        this.textBox.TabIndex = 0;
        // 
        // Form1
        this.Controls.Add(this.textBox);
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.TextBox textBox;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.EnableRTLMirroring();
        Application.Run(new Form1());
    }
}


           
          


Bind DataSet to TextBox

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using System.Data.SqlClient;

public class Form1 : Form {
    public Form1() {
        InitializeComponent();

        DataSet ds = CreateDataSet();

        textBox1.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");
    }

    private DataSet CreateDataSet() {
        string customers = "SELECT * FROM Products";
        DataSet ds = new DataSet();

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
            SqlDataAdapter da = new SqlDataAdapter(customers, con);

            da.Fill(ds, "Products");
        }

        return ds;
    }
    private void InitializeComponent() {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.textBox1.Location = new System.Drawing.Point(12, 12);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(370, 20);
        this.textBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(394, 44);
        this.Controls.Add(this.textBox1);
        this.Name = "Form1";
        this.Text = "SimpleDataBinding";
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.TextBox textBox1;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

    


Fill data from Database to ListBox

image_pdfimage_print
   
 


using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class Form1 : System.Windows.Forms.Form {
    SqlConnection cn = new SqlConnection("data source=.;database=biblio;uid=admin;pwd=pw");
    SqlDataAdapter da = new SqlDataAdapter();
    string strSQL = "Select Title, PubID from Titles";
    SqlCommand cmd;
    SqlDataReader Dr;

    System.Windows.Forms.Button Button1 = new System.Windows.Forms.Button();
    System.Windows.Forms.ListBox ListBox1  = new System.Windows.Forms.ListBox();
    System.Windows.Forms.TextBox TextBox1  = new System.Windows.Forms.TextBox();

    public Form1() {
        cmd = new SqlCommand(strSQL, cn);
        this.SuspendLayout();

        this.Button1.Location = new System.Drawing.Point(136, 248);
        this.Button1.Size = new System.Drawing.Size(144, 32);
        this.Button1.Text = "Get Data";
        this.Button1.Click += new System.EventHandler(this.Button1_Click);

        this.ListBox1.Location = new System.Drawing.Point(48, 64);
        this.ListBox1.Size = new System.Drawing.Size(312, 160);

        this.TextBox1.Location = new System.Drawing.Point(48, 24);
        this.TextBox1.Size = new System.Drawing.Size(328, 20);
        this.TextBox1.Text = "Hit";

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(408, 293);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Button1,
                                                                          this.ListBox1,
                                                                          this.TextBox1});
        this.ResumeLayout(false);
    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void Button1_Click(object sender, System.EventArgs e) {
        cn.Open(); 
        cmd.CommandText = strSQL + "'" + TextBox1.Text + "%'";
        Dr = cmd.ExecuteReader();
        ListBox1.Items.Clear(); 
        ListBox1.BeginUpdate();  

        while (Dr.Read()){
            ListBox1.Items.Add(Dr.GetString(0) + " - " + Dr.GetInt32(1).ToString());
        }

        ListBox1.EndUpdate();  

        Dr.Close();  

    }
}

    


Binding database table to ListBox and TextBox, link ListBox and TextBox

image_pdfimage_print
   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

    public class Form1 : System.Windows.Forms.Form {
      private System.Windows.Forms.ListBox listBox1;
      private System.Windows.Forms.TextBox textBox1;
      private System.Windows.Forms.TextBox textBox2;
      private System.Data.DataSet dataSet1;

      private System.ComponentModel.Container components = null;

      public Form1() {
        InitializeComponent();
      }

      private void InitializeComponent() {
         this.listBox1 = new System.Windows.Forms.ListBox();
         this.textBox1 = new System.Windows.Forms.TextBox();
         this.textBox2 = new System.Windows.Forms.TextBox();
         this.dataSet1 = new System.Data.DataSet();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
         this.SuspendLayout();

         this.listBox1.Location = new System.Drawing.Point(8, 8);
         this.listBox1.Name = "listBox1";
         this.listBox1.Size = new System.Drawing.Size(232, 95);
         this.listBox1.TabIndex = 0;

         this.textBox1.Location = new System.Drawing.Point(8, 120);
         this.textBox1.Name = "textBox1";
         this.textBox1.TabIndex = 1;
         this.textBox1.Text = "textBox1";

         this.textBox2.Location = new System.Drawing.Point(8, 152);
         this.textBox2.Name = "textBox2";
         this.textBox2.TabIndex = 2;
         this.textBox2.Text = "textBox2";

         this.dataSet1.DataSetName = "NewDataSet";
         this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");

         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(256, 188);
         this.Controls.Add(this.textBox2);
         this.Controls.Add(this.textBox1);
         this.Controls.Add(this.listBox1);
         this.Name = "Form1";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.Form1_Load);
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
         this.ResumeLayout(false);

      }
      static void Main() {
         Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e) {
         string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
  
         string sql = @"select firstname, lastname from employee ";

         SqlConnection conn = new SqlConnection(connString);
         SqlDataAdapter da = new SqlDataAdapter(sql, conn);
         da.Fill(dataSet1, "employee");
         DataTable dt = dataSet1.Tables["employee"];

         listBox1.DataSource = dt;
         listBox1.DisplayMember = "firstname";

         textBox1.DataBindings.Add("text", dt, "firstname");

         textBox2.DataBindings.Add("text", dt, "lastname");

      }
    }



           
          


Bind DataSet to Label

image_pdfimage_print
   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

   public class Form1 : System.Windows.Forms.Form {
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.Data.DataSet dataSet1;
 
      private System.ComponentModel.Container components = null;

      public Form1(){ 
 
         InitializeComponent();
      }

      private void InitializeComponent() {
         this.label1 = new System.Windows.Forms.Label();
         this.label2 = new System.Windows.Forms.Label();
         this.dataSet1 = new System.Data.DataSet();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
         this.SuspendLayout();
 
         this.label1.Location = new System.Drawing.Point(16, 8);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(72, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "label1";
 
         this.label2.Location = new System.Drawing.Point(16, 40);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(72, 16);
         this.label2.TabIndex = 1;
         this.label2.Text = "label2";
 
         this.dataSet1.DataSetName = "NewDataSet";
         this.dataSet1.Locale = new System.Globalization.CultureInfo("en-GB");
 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(128, 69);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.label2,
                                                                      this.label1});
         this.Name = "Form1";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.Form1_Load);
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
         this.ResumeLayout(false);

      }
 
      static void Main() {
         Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e) {
         string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    
         string SQL = @"select * from employee";

         SqlConnection Conn = new SqlConnection(connString);
         SqlDataAdapter da = new SqlDataAdapter(SQL, Conn);

         da.Fill(dataSet1, "employee");

         // Bind the Label's Text property to the ProductID of the Products table
         label1.DataBindings.Add("Text", dataSet1, "employee.ID");
         // Bind the second label's Text property to the UnitPrice column
         label2.DataBindings.Add("Text", dataSet1, "employee.firstname");
      }
   }



           
          


Bind two DataGrids with many to many mapping

image_pdfimage_print
   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

    public class Form1 : System.Windows.Forms.Form{
      private System.Windows.Forms.DataGrid dataGrid1;
      private System.Data.DataSet dataSet1;
      private System.ComponentModel.Container components = null;

      public Form1() {
        InitializeComponent();
      }
      private void InitializeComponent(){
        this.dataGrid1 = new System.Windows.Forms.DataGrid();
        this.dataSet1 = new System.Data.DataSet();
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
        this.SuspendLayout();

        this.dataGrid1.DataMember = "";
        this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.dataGrid1.Location = new System.Drawing.Point(0, 0);
        this.dataGrid1.Name = "dataGrid1";
        this.dataGrid1.Size = new System.Drawing.Size(400, 200);
        this.dataGrid1.TabIndex = 0;

        this.dataSet1.DataSetName = "NewDataSet";
        this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(400, 196);
        this.Controls.Add(this.dataGrid1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
        this.ResumeLayout(false);
      }
      [STAThread]
      static void Main() {
        Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e) {
         string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string qry1 = @"select * from employee ";
         string qry2 = @"select * from order";

         string sql = qry1 + qry2;

         SqlConnection conn = new SqlConnection(connString);
         SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    
         da.TableMappings.Add("Table", "employee");
         da.TableMappings.Add("Table1", "order");
         da.Fill(dataSet1);

         DataRelation dr = new DataRelation(
            "employeeorders",
            dataSet1.Tables[0].Columns["employeeid"], 
            dataSet1.Tables[1].Columns["employeeid"]
            );
         dataSet1.Relations.Add(dr);

         dataGrid1.SetDataBinding(dataSet1, "employees");

      }
    }



           
          


DataGrid Update: edit a table by binding component

image_pdfimage_print
   

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

    public class Form1 : System.Windows.Forms.Form {
      private System.Windows.Forms.DataGrid dataGrid1;
      private System.Windows.Forms.Button buttonUpdate;
      private System.Data.DataSet dataSet1;
      private System.Data.SqlClient.SqlCommand sqlCommand1;
      private System.ComponentModel.Container components = null;

      private SqlCommandBuilder cb;
      private SqlDataAdapter da;

      public Form1() {
         InitializeComponent();
      }
      private void InitializeComponent(){
         this.dataGrid1 = new System.Windows.Forms.DataGrid();
         this.buttonUpdate = new System.Windows.Forms.Button();
         this.dataSet1 = new System.Data.DataSet();
         this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
         ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
         this.SuspendLayout();

         this.dataGrid1.DataMember = "";
         this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
         this.dataGrid1.Location = new System.Drawing.Point(8, 8);
         this.dataGrid1.Name = "dataGrid1";
         this.dataGrid1.Size = new System.Drawing.Size(440, 208);
         this.dataGrid1.TabIndex = 0;

         this.buttonUpdate.Location = new System.Drawing.Point(191, 232);
         this.buttonUpdate.Name = "buttonUpdate";
         this.buttonUpdate.TabIndex = 1;
         this.buttonUpdate.Text = "Update";
         this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click);

         this.dataSet1.DataSetName = "NewDataSet";
         this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");

         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(456, 272);
         this.Controls.Add(this.buttonUpdate);
         this.Controls.Add(this.dataGrid1);
         this.Name = "Form1";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.Form1_Load);
         ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
         this.ResumeLayout(false);

      }
      static void Main() {
         Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e){
         string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string sql = @"select * from employee ";
         SqlConnection conn = new SqlConnection(connString);
         sqlCommand1 = new SqlCommand(sql, conn);

         da = new SqlDataAdapter();
         da.SelectCommand = sqlCommand1;

         cb = new SqlCommandBuilder(da);

         da.Fill(dataSet1, "employee");

         dataGrid1.SetDataBinding(dataSet1, "employee");
      }

      private void buttonUpdate_Click(object sender, System.EventArgs e) {
         da.Update(dataSet1, "employee"); 
      }
    }