Link two DataTable in a DataGrid : DataGrid « ADO.Net « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » DataGrid 
32.52.2.Link two DataTable in a DataGrid
Link two DataTable in a DataGrid
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;

public class DataTableLinkinDataGrid : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid dataGrid1;

  private System.Data.DataSet dtSet;

  public DataTableLinkinDataGrid()
  {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(168);
    this.dataGrid1.Size = new System.Drawing.Size(384240);
    // 
    // DataTableLinkinDataGrid
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(416273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.dataGrid1});
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);
    
    CreateCustomersTable();
        CreateOrdersTable();
        BindData();
  }

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

  private void CreateCustomersTable()
  {
    System.Data.DataTable custTable = new DataTable("Customers");
    DataColumn dtColumn;
    DataRow myDataRow;

    dtColumn = new DataColumn();
    dtColumn.DataType = System.Type.GetType("System.Int32");
    dtColumn.ColumnName = "id";
    dtColumn.Caption = "Cust ID";
    dtColumn.ReadOnly = true;
    dtColumn.Unique = true;
    custTable.Columns.Add(dtColumn);

    dtColumn = new DataColumn();
    dtColumn.DataType = System.Type.GetType("System.String");
    dtColumn.ColumnName = "Name";
    dtColumn.Caption = "Cust Name";
    dtColumn.AutoIncrement = false;
    dtColumn.ReadOnly = false;
    dtColumn.Unique = false;
    custTable.Columns.Add(dtColumn);


    dtColumn = new DataColumn();
    dtColumn.DataType = System.Type.GetType("System.String");
    dtColumn.ColumnName = "Address";
    dtColumn.Caption = "Address";
    dtColumn.ReadOnly = false;
    dtColumn.Unique = false;
    custTable.Columns.Add(dtColumn);

    DataColumn[] PrimaryKeyColumns = new DataColumn[1];
    PrimaryKeyColumns[0= custTable.Columns["id"];
    custTable.PrimaryKey = PrimaryKeyColumns;

    dtSet = new DataSet("Customers");
    dtSet.Tables.Add(custTable);

    myDataRow = custTable.NewRow();
    myDataRow["id"1001;
    myDataRow["Address""address 1";
    myDataRow["Name""G";
    custTable.Rows.Add(myDataRow);

    myDataRow = custTable.NewRow();
    myDataRow["id"1002;
    myDataRow["Name""R";
    myDataRow["Address""address 2";
    custTable.Rows.Add(myDataRow);

    myDataRow = custTable.NewRow();
    myDataRow["id"1003;
    myDataRow["Name""M";
    myDataRow["Address""address 3";
    custTable.Rows.Add(myDataRow);
    
  }

  private void CreateOrdersTable()
  {
    DataTable ordersTable = new DataTable("Orders");
    DataColumn dtColumn;
    DataRow dtRow;

    dtColumn = new DataColumn();
    dtColumn.DataType= System.Type.GetType("System.Int32");
    dtColumn.ColumnName = "OrderId";
    dtColumn.AutoIncrement = true;
    dtColumn.Caption = "Order ID";
    dtColumn.ReadOnly = true;
    dtColumn.Unique = true;
    ordersTable.Columns.Add(dtColumn);

    dtColumn = new DataColumn();
    dtColumn.DataType= System.Type.GetType("System.String");
    dtColumn.ColumnName = "Name";
    dtColumn.Caption = "Item Name";
    ordersTable.Columns.Add(dtColumn);

    // Create CustId column which reprence CustId from 
    // the custTable
    dtColumn = new DataColumn();
    dtColumn.DataType= System.Type.GetType("System.Int32");
    dtColumn.ColumnName = "CustId";
    dtColumn.AutoIncrement = false;
    dtColumn.Caption = "CustId";
    dtColumn.ReadOnly = false;
    dtColumn.Unique = false;
    ordersTable.Columns.Add(dtColumn);

    dtColumn = new DataColumn();
    dtColumn.DataType= System.Type.GetType("System.String");
    dtColumn.ColumnName = "Description";
    dtColumn.Caption = "Description Name";
    ordersTable.Columns.Add(dtColumn);

    dtSet.Tables.Add(ordersTable);

    dtRow = ordersTable.NewRow();
    dtRow["OrderId"0;
    dtRow["Name""Book";
    dtRow["CustId"1001 ;
    dtRow["Description""desc 1" ;
    ordersTable.Rows.Add(dtRow);
    
    dtRow = ordersTable.NewRow();
    dtRow["OrderId"1;
    dtRow["Name""Book";
    dtRow["CustId"1001 ;
    dtRow["Description""desc 2" ;
    ordersTable.Rows.Add(dtRow);

  }

    private void BindData()
    {
        DataRelation dtRelation;
        DataColumn CustCol = dtSet.Tables["Customers"].Columns["id"];
        DataColumn orderCol = dtSet.Tables["Orders"].Columns["CustId"];
        
        dtRelation = new DataRelation("CustOrderRelation", CustCol, orderCol);
        dtSet.Tables["Orders"].ParentRelations.Add(dtRelation);

        dataGrid1.SetDataBinding(dtSet,"Customers");
    }       


}
32.52.DataGrid
32.52.1.Load data in DataTable to DataGridLoad data in DataTable to DataGrid
32.52.2.Link two DataTable in a DataGridLink two DataTable in a DataGrid
32.52.3.Load Data to DataGridLoad Data to DataGrid
32.52.4.DataGrid View: on data error
32.52.5.Data binding for Multiple Controls
32.52.6.Binding DataSet to DataGrid
32.52.7.Programmatic Data Display
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.