DataSet.Relations.Add : DataSet « System.Data « C# / C Sharp by API






DataSet.Relations.Add

  

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

    private void getData_Click(object sender, EventArgs e) {
        string orders = "SELECT * FROM Orders";
        string customers = "SELECT * FROM Customers";

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {
            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter(customers, con);
            da.Fill(ds, "Customers");

            da = new SqlDataAdapter(orders, con);
            da.Fill(ds, "Orders");

            ds.Relations.Add("CustomerOrders",
                ds.Tables["Customers"].Columns["CustomerID"],
                ds.Tables["Orders"].Columns["CustomerID"]);

            dataGrid1.SetDataBinding(ds, "Customers");
        }
    }
    private void InitializeComponent() {
        this.getData = new System.Windows.Forms.Button();
        this.dataGrid1 = new System.Windows.Forms.DataGrid();
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
        this.SuspendLayout();

        this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.getData.Location = new System.Drawing.Point(316, 299);
        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.dataGrid1.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.dataGrid1.DataMember = "";
        this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.dataGrid1.Location = new System.Drawing.Point(12, 12);
        this.dataGrid1.Size = new System.Drawing.Size(379, 281);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(403, 334);
        this.Controls.Add(this.dataGrid1);
        this.Controls.Add(this.getData);
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.Button getData;
    private System.Windows.Forms.DataGrid dataGrid1;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

   
    
  








Related examples in the same category

1.new DataSet(String data)
2.DataSet.Delete
3.DataSet.Merge
4.DataSet.MergeFailed
5.DataSet.Read
6.DataSet.ReadXml
7.DataSet.Tables
8.DataSet.WriteXml