‘DataGrid’ et Liaison MultiTable: Exemple de rélation n et n

Author:

Recherche,dans,la,base,de,données:,non,édité
{filelink=16158}

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

namespace Base_de_Données
{
    public class DataGridLiaisonmultiTable : System.Windows.Forms.Form
    {
      private System.Windows.Forms.DataGrid tableDonnées;
      private System.Data.DataSet dataSet1;

      public DataGridLiaisonmultiTable()
      {
        InitializeComponent();
      }
      private void InitializeComponent(){
        this.tableDonnées = new System.Windows.Forms.DataGrid();
        this.dataSet1 = new System.Data.DataSet();
        ((System.ComponentModel.ISupportInitialize)(this.tableDonnées)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
        this.SuspendLayout();

        this.tableDonnées.DataMember = "";
        this.tableDonnées.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.tableDonnées.Location = new System.Drawing.Point(0, 0);
        this.tableDonnées.Name = "dataGrid1";
        this.tableDonnées.Size = new System.Drawing.Size(400, 200);
        this.tableDonnées.TabIndex = 0;

        this.dataSet1.DataSetName = "NewDataSet";
        this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(400, 196);
        this.Controls.Add(this.tableDonnées);
        this.Name = "DataGridLiaisonmultiTable";
        this.Text = "DataGrid Liaison MultiTable";
        this.Load += new System.EventHandler(this.DataGridLiaisonmultiTable_Load);
        ((System.ComponentModel.ISupportInitialize)(this.tableDonnées)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
        this.ResumeLayout(false);
      }
      [STAThread]
      static void Main() {
        Application.Run(new DataGridLiaisonmultiTable());
      }

      private void DataGridLiaisonmultiTable_Load(object sender, System.EventArgs e) {
         string connString =@"server=(local)SQLEXPRESS;database=MesExemples.com;Integrated Security=SSPI";
         string requête1 = "select * from Clients ";
         string requête2 = "select * from Achats";

         string sql = requête1 + requête2;

         SqlConnection conn = new SqlConnection(connString);
         SqlDataAdapter da = new SqlDataAdapter(sql, conn);

         da.TableMappings.Add("Table", "Clients");
         da.TableMappings.Add("Table1", "Achats");
         da.Fill(dataSet1);

        // Définir la table et le champs de liaison
         DataRelation dr = new DataRelation(
            "AchatsClients",
            dataSet1.Tables[0].Columns["IDClient"], // Clé primaire de la table Client
            dataSet1.Tables[1].Columns["IDClient"] //  Clé etrangère de la Table AchatsClient
            );
         dataSet1.Relations.Add(dr);

         tableDonnées.SetDataBinding(dataSet1, "employees");

      }
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *