Set up relation between two tables : DataGrid « Database ADO.net « C# / C Sharp






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


           
       








Related examples in the same category

1.Fill a DataGrid
2.Set SelectCommand
3.Bind related tables
4.Set Data Binding with DataViewManager
5.Set DataSource from DataSet