Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn

   

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();
    }

    private void getData_Click(object sender, EventArgs e) {
        getData.Enabled = false;
        using (SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["northwind"].ConnectionString)) {
            string select = "SELECT EmployeeID, FirstName, LastName, Photo, IsNull(ReportsTo,0) as ReportsTo FROM Employees";
            SqlDataAdapter da = new SqlDataAdapter(select, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Employees");
            select = "SELECT EmployeeID, FirstName + ' ' + LastName as Name FROM Employees union select 0,'(None)'";
            da = new SqlDataAdapter(select, con);
            da.Fill(ds, "Managers");
            SetupColumns(ds);
            dataGridView.AutoGenerateColumns = false;
            dataGridView.DataSource = ds.Tables["Employees"];
            dataGridView.AutoSizeRows(DataGridViewAutoSizeRowsMode.HeaderAndColumnsAllRows);
        }
    }

    private void SetupColumns(DataSet ds) {
        DataGridViewTextBoxColumn forenameColumn = new DataGridViewTextBoxColumn();
        forenameColumn.DataPropertyName = "FirstName";
        forenameColumn.HeaderText = "Forename";
        forenameColumn.ValueType = typeof(string);
        forenameColumn.Frozen = true;
        dataGridView.Columns.Add(forenameColumn);

        DataGridViewTextBoxColumn surnameColumn = new DataGridViewTextBoxColumn();
        surnameColumn.DataPropertyName = "LastName";
        surnameColumn.HeaderText = "Surname";
        surnameColumn.Frozen = true;
        surnameColumn.ValueType = typeof(string);
        dataGridView.Columns.Add(surnameColumn);

        DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
        photoColumn.DataPropertyName = "Photo";
        photoColumn.Width = 200;
        photoColumn.HeaderText = "Image";
        photoColumn.ReadOnly = true;
        photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
        dataGridView.Columns.Add(photoColumn);

        DataGridViewComboBoxColumn reportsToColumn = new DataGridViewComboBoxColumn();
        reportsToColumn.HeaderText = "Reports To";
        reportsToColumn.DataSource = ds.Tables["Managers"];
        reportsToColumn.DisplayMember = "Name";
        reportsToColumn.ValueMember = "EmployeeID";
        reportsToColumn.DataPropertyName = "ReportsTo";
        dataGridView.Columns.Add(reportsToColumn);

    }
    private void InitializeComponent() {
        this.getData = new System.Windows.Forms.Button();
        this.dataGridView = new System.Windows.Forms.DataGridView();
        this.SuspendLayout();
        // 
        // getData
        // 
        this.getData.Location = new System.Drawing.Point(661, 544);
        this.getData.Name = "getData";
        this.getData.TabIndex = 0;
        this.getData.Text = "Get Data";
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // dataGridView
        // 
        this.dataGridView.AllowUserToAddRows = false;
        this.dataGridView.AllowUserToDeleteRows = false;
        this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView.Location = new System.Drawing.Point(13, 13);
        this.dataGridView.Name = "dataGridView";
        this.dataGridView.Size = new System.Drawing.Size(722, 522);
        this.dataGridView.TabIndex = 1;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(748, 579);
        this.Controls.Add(this.dataGridView);
        this.Controls.Add(this.getData);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.Button getData;
    private System.Windows.Forms.DataGridView dataGridView;

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

           
          


Bind List to DataGridView

   

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


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;

public class Person {
    public Person(string name, Sex sex, DateTime dob) {
        _name = name;
        _sex = sex;
        _dateOfBirth = dob;
    }

    public string Name {
        get { return _name; }
        set { _name = value; }
    }

    public Sex Sex {
        get { return _sex; }
        set { _sex = value; }
    }

    public DateTime DateOfBirth {
        get { return _dateOfBirth; }
        set { _dateOfBirth = value; }
    }

    private string _name;
    private Sex _sex;
    private DateTime _dateOfBirth;
}

public enum Sex {
    Male,
    Female
}

class PersonList : List<Person> {
}
public class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void getData_Click(object sender, EventArgs e) {
        PersonList people = new PersonList();

        people.Add(new Person("F", Sex.Male, new DateTime(1970, 12, 14)));
        people.Add(new Person("B", Sex.Male, new DateTime(1976, 10, 29)));
        people.Add(new Person("J", Sex.Male, new DateTime(1945, 5, 17)));
        people.Add(new Person("J", Sex.Female, new DateTime(1982, 1, 3)));

        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = people;
    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(13, 13);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(267, 217);
        this.dataGridView1.TabIndex = 0;
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(205, 236);
        this.getData.Size = new System.Drawing.Size(75, 23);
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        this.Text = "DataSourceGenericCollection";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button getData;

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


           
          


Setup Columns for DataGridView

   




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;

// CustomDataGridView

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

    private void getData_Click(object sender, EventArgs e) {
        getData.Enabled = false;

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
            string select = "SELECT EmployeeID, FirstName, LastName, Photo, IsNull(ReportsTo,0) as ReportsTo FROM Employees";

            SqlDataAdapter da = new SqlDataAdapter(select, con);

            DataSet ds = new DataSet();

            da.Fill(ds, "Employees");

            select = "SELECT EmployeeID, FirstName + &#039; &#039; + LastName as Name FROM Employees union select 0,&#039;(None)&#039;";

            da = new SqlDataAdapter(select, con);
            da.Fill(ds, "Managers");
            SetupColumns(ds);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["Employees"];
            dataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
        }
    }
    private void SetupColumns(DataSet ds) {
        DataGridViewTextBoxColumn forenameColumn = new DataGridViewTextBoxColumn();
        forenameColumn.DataPropertyName = "FirstName";
        forenameColumn.HeaderText = "Forename";
        forenameColumn.ValueType = typeof(string);
        forenameColumn.Frozen = true;
        dataGridView1.Columns.Add(forenameColumn);

        DataGridViewTextBoxColumn surnameColumn = new DataGridViewTextBoxColumn();
        surnameColumn.DataPropertyName = "LastName";
        surnameColumn.HeaderText = "Surname";
        surnameColumn.Frozen = true;
        surnameColumn.ValueType = typeof(string);
        dataGridView1.Columns.Add(surnameColumn);

        DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
        photoColumn.DataPropertyName = "Photo";
        photoColumn.Width = 200;
        photoColumn.HeaderText = "Image";
        photoColumn.ReadOnly = true;
        photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
        dataGridView1.Columns.Add(photoColumn);

        DataGridViewComboBoxColumn reportsToColumn = new DataGridViewComboBoxColumn();
        reportsToColumn.HeaderText = "Reports To";
        reportsToColumn.DataSource = ds.Tables["Managers"];
        reportsToColumn.DisplayMember = "Name";
        reportsToColumn.ValueMember = "EmployeeID";
        reportsToColumn.DataPropertyName = "ReportsTo";
        dataGridView1.Columns.Add(reportsToColumn);

    }
    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(13, 13);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(507, 372);
        this.dataGridView1.TabIndex = 0;
        // 
        // getData
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(445, 391);
        this.getData.Name = "getData";
        this.getData.Size = new System.Drawing.Size(75, 23);
        this.getData.TabIndex = 1;
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(532, 426);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "CustomDataGridView";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }



    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button getData;

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

           
          


Data Source with Generic Collection

   



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

public class Person {
    public Person(string name, Sex sex, DateTime dob) {
        _name = name;
        _sex = sex;
        _dateOfBirth = dob;
    }

    public string Name {
        get { return _name; }
        set { _name = value; }
    }

    public Sex Sex {
        get { return _sex; }
        set { _sex = value; }
    }

    public DateTime DateOfBirth {
        get { return _dateOfBirth; }
        set { _dateOfBirth = value; }
    }

    private string _name;
    private Sex _sex;
    private DateTime _dateOfBirth;
}

public enum Sex {
    Male,
    Female
}
class PersonList : List<Person> {
}

public class SexDisplay {
    public SexDisplay(Sex s) {
        _sex = s;
        _caption = s.ToString();
    }

    public Sex Sex {
        get { return _sex; }
    }

    public string Caption {
        get { return _caption; }
    }

    private Sex _sex;
    private string _caption;
}

public class SexList : List<SexDisplay> {
}
class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void getData_Click(object sender, EventArgs e) {
        PersonList people = new PersonList();

        people.Add(new Person("F", Sex.Male, new DateTime(1970, 12, 14)));
        people.Add(new Person("B", Sex.Male, new DateTime(1976, 10, 29)));
        people.Add(new Person("J", Sex.Male, new DateTime(1945, 5, 17)));
        people.Add(new Person("J", Sex.Female, new DateTime(1982, 1, 3)));

        dataGridView.AutoGenerateColumns = true;
        dataGridView.DataSource = people;
    }

    private void Stuff() {
        DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
        nameColumn.DataPropertyName = "Name";
        nameColumn.Name = "Name";
        nameColumn.HeaderText = "Name";
        nameColumn.ValueType = typeof(string);
        dataGridView.Columns.Add(nameColumn);

        DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
        buttonCol.Text = "Hello";
        buttonCol.Name = "Hello";
        buttonCol.HeaderText = "Hello";
        buttonCol.ValueType = typeof(string);
        buttonCol.DataPropertyName = "Name";
        dataGridView.Columns.Add(buttonCol);

        DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();
        checkCol.HeaderText = "Blah";
        dataGridView.Columns.Add(checkCol);

        DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
        comboCol.HeaderText = "Combo";
        SexList sl = new SexList();
        sl.Add(new SexDisplay(Sex.Male));
        sl.Add(new SexDisplay(Sex.Female));
        comboCol.DataSource = sl;
        comboCol.DisplayMember = "Caption";
        comboCol.ValueMember = "Sex";
        comboCol.DataPropertyName = "Sex";
        dataGridView.Columns.Add(comboCol);

        DataGridViewLinkColumn linkCol = new DataGridViewLinkColumn();
        linkCol.HeaderText = "Link";
        linkCol.Text = "Bibble";
        dataGridView.Columns.Add(linkCol);
        linkCol.DataPropertyName = "DateOfBirth";

    }

    private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) {
        int i = 0;
    }

    private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {
        int i = 0;
    }
    private void InitializeComponent() {
        this.getData = new System.Windows.Forms.Button();
        this.dataGridView = new System.Windows.Forms.DataGridView();
        this.SuspendLayout();
        // 
        // getData
        // 
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(776, 600);
        this.getData.Name = "getData";
        this.getData.TabIndex = 0;
        this.getData.Text = "Get Data";
        this.getData.Click += new System.EventHandler(this.getData_Click);
        // 
        // dataGridView
        // 
        this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView.Location = new System.Drawing.Point(13, 13);
        this.dataGridView.Name = "dataGridView";
        this.dataGridView.Size = new System.Drawing.Size(837, 573);
        this.dataGridView.TabIndex = 1;
        this.dataGridView.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView_CellClick);
        this.dataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView_DataError);
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(863, 635);
        this.Controls.Add(this.dataGridView);
        this.Controls.Add(this.getData);
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.Button getData;
    private System.Windows.Forms.DataGridView dataGridView;

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

}

           
          


Bind DataGridView to Array

   


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

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

    private void getData_Click(object sender, EventArgs e) {
        Item[] items = new Item[] { new Item ( "One" ) , new Item ( "Two" ) , new Item ( "Three" ) };
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = items;
    }

    protected class Item {
        public Item(string text) {
            m_text = text;
        }

        public string Text {
            get { return m_text; }
        }

        private string m_text;
    }

    private void InitializeComponent() {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.getData = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(12, 12);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(268, 223);
        this.dataGridView1.TabIndex = 0;
        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(204, 242);
        this.getData.Name = "getData";
        this.getData.Size = new System.Drawing.Size(75, 23);
        this.getData.TabIndex = 1;
        this.getData.Text = "Get Data";
        this.getData.UseVisualStyleBackColor = true;
        this.getData.Click += new System.EventHandler(this.getData_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Controls.Add(this.getData);
        this.Controls.Add(this.dataGridView1);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Button getData;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}


           
          


Set DataSource from DataSet

   





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 {
    const string strConnect = "data source=".";database=&#039;biblio&#039;;uid="admin";pwd=pw";
    string strQuery = "Select Title, Price from Titles where Title like &#039;Hit%&#039;";
    SqlConnection cn = new SqlConnection(strConnect); // = new SqlDataAdapter();
    DataSet ds = new DataSet();

    internal System.Windows.Forms.Button Button1;
    internal System.Windows.Forms.DataGrid DataGrid1;
    private System.Data.SqlClient.SqlDataAdapter da;

    public Form1() {
        this.Button1 = new System.Windows.Forms.Button();
        this.DataGrid1 = new System.Windows.Forms.DataGrid();
        this.da = new System.Data.SqlClient.SqlDataAdapter();
        ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).BeginInit();
        this.SuspendLayout();

        this.Button1.Location = new System.Drawing.Point(168, 266);
        this.Button1.Size = new System.Drawing.Size(96, 32);
        this.Button1.Text = "Get Data";
        this.Button1.Click += new System.EventHandler(this.Button1_Click);

        this.DataGrid1.DataMember = "";
        this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.DataGrid1.Location = new System.Drawing.Point(8, 18);
        this.DataGrid1.Size = new System.Drawing.Size(392, 232);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(408, 317);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Button1,
                                                                          this.DataGrid1});
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).EndInit();
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void Form1_Load(object sender, System.EventArgs e) {
        try {
            cn.Open();
            da.SelectCommand = new SqlCommand(strQuery, cn);
        } catch (SqlException ex) {
            Console.WriteLine(ex.ToString());
        }
    }

    private void Button1_Click(object sender, System.EventArgs e) {
        da.Fill(ds, "Titles and Price");
        DataGrid1.DataSource = ds.Tables["Titles and Price"];
    }
}


           
          


Set up relation between two tables

   

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 grdOrders;
    private System.Windows.Forms.DataGrid grdOrderDetails;
    private System.Windows.Forms.DataGrid grdCustomers;
    public Form1() {
        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"]);

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

        this.grdCustomers = new System.Windows.Forms.DataGrid();
        this.grdOrders = new System.Windows.Forms.DataGrid();
        this.grdOrderDetails = new System.Windows.Forms.DataGrid();
        ((System.ComponentModel.ISupportInitialize)(this.grdCustomers)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.grdOrders)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.grdOrderDetails)).BeginInit();
        this.SuspendLayout();
        // 
        this.grdCustomers.AllowNavigation = false;
        this.grdCustomers.DataMember = "";
        this.grdCustomers.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.grdCustomers.Location = new System.Drawing.Point(40, 16);
        this.grdCustomers.Name = "grdCustomers";
        this.grdCustomers.Size = new System.Drawing.Size(448, 152);
        this.grdCustomers.TabIndex = 0;
        // 
        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;
        // 
        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;
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(528, 483);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.grdOrderDetails,
                                                                  this.grdOrders,
                                                                  this.grdCustomers});
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.grdCustomers)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.grdOrders)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.grdOrderDetails)).EndInit();
        this.ResumeLayout(false);

    }

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