DataGrid data binding : DataBinding DataGrid « ADO.Net « C# / CSharp Tutorial






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

  public class SimpleTest : System.Windows.Forms.Form
  {
    private System.Windows.Forms.DataGrid dataGrid1;
    public SimpleTest()
    {
      this.dataGrid1 = new System.Windows.Forms.DataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
      this.SuspendLayout();
      // 
      // dataGrid1
      // 
      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(8, 8);
      this.dataGrid1.Name = "dataGrid1";
      this.dataGrid1.Size = new System.Drawing.Size(276, 228);
      this.dataGrid1.TabIndex = 0;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(292, 245);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.dataGrid1});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.Name = "Form1";
      this.Text = "Simple Data Binding";
      this.Load += new System.EventHandler(this.Form1_Load);
      ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
      this.ResumeLayout(false);

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


    private void Form1_Load(object sender, System.EventArgs e)
    {
        string connectionString = @"Data Source=localhost;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.FillSchema(ds,SchemaType.Mapped , "Customers");
        adapter.Fill(ds, "Customers");

        com.CommandText = "SELECT * FROM Products";
        adapter.FillSchema(ds,SchemaType.Mapped , "Products");
        adapter.Fill(ds, "Products");

        com.CommandText = "SELECT * FROM Suppliers";
        adapter.FillSchema(ds,SchemaType.Mapped , "Suppliers");
        adapter.Fill(ds, "Suppliers");
        con.Close();
        dataGrid1.DataSource=ds;
      }
  }








32.53.DataBinding DataGrid
32.53.1.Data Binding: DataGridData Binding: DataGrid
32.53.2.DataGrid Column Style settingDataGrid Column Style setting
32.53.3.DataGrid Column Asssignment for data bindingDataGrid Column Asssignment for data binding
32.53.4.DataGrid DataSource SettingDataGrid DataSource Setting
32.53.5.Load data from a select statement to DataGridLoad data from a select statement to DataGrid
32.53.6.DataGrid Relation
32.53.7.DataGrid data binding