Enregistrer les Modification d’un ‘DataGrid’ dans la Base de données

Author:

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

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 MiseAJourDataGrid : System.Windows.Forms.Form
	{
      private System.Windows.Forms.DataGrid tableDonnées;
      private System.Windows.Forms.Button btnModifier;
      private System.Data.DataSet dataSet1;
      private System.Data.SqlClient.SqlCommand commandeSQL;

      private SqlCommandBuilder cb;
      private SqlDataAdapter da;

      public MiseAJourDataGrid() {
         InitializeComponent();
      }
      private void InitializeComponent(){
         this.tableDonnées = new System.Windows.Forms.DataGrid();
         this.btnModifier = new System.Windows.Forms.Button();
         this.dataSet1 = new System.Data.DataSet();
         this.commandeSQL = new System.Data.SqlClient.SqlCommand();
         ((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(8, 8);
         this.tableDonnées.Name = "dataGrid1";
         this.tableDonnées.Size = new System.Drawing.Size(440, 208);
         this.tableDonnées.TabIndex = 0;

         this.btnModifier.Location = new System.Drawing.Point(191, 232);
         this.btnModifier.Size = new Size(120, 40);
         this.btnModifier.Name = "buttonUpdate";
         this.btnModifier.TabIndex = 1;
         this.btnModifier.Text = "Enregistrer les Modification";
         this.btnModifier.Click += new System.EventHandler(this.buttonUpdate_Click);

         this.dataSet1.DataSetName = "NewDataSet";
         this.dataSet1.Locale = new System.Globalization.CultureInfo("fr");

         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(456, 272);
         this.Controls.Add(this.btnModifier);
         this.Controls.Add(this.tableDonnées);
         this.Name = "MiseAJourDataGrid";
         this.Text = "MiseAJourDataGrid";
         this.Load += new System.EventHandler(this.MiseAJourDataGrid_Load);
         ((System.ComponentModel.ISupportInitialize)(this.tableDonnées)).EndInit();
         ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
         this.ResumeLayout(false);

      }
      static void Main()
	  {
         Application.Run(new MiseAJourDataGrid());
      }

      private void MiseAJourDataGrid_Load(object sender, System.EventArgs e){
         string chaineConnexion = "server=(local);database=MesExemples.com;Integrated Security=SSPI";
         string requete = "select ID, Société, Adresse, Activites, telephone from Clients";
         SqlConnection conn = new SqlConnection(chaineConnexion);
         commandeSQL = new SqlCommand(requete, conn);

         da = new SqlDataAdapter();
         da.SelectCommand = commandeSQL;

         cb = new SqlCommandBuilder(da);

         da.Fill(dataSet1, "Clients");

         tableDonnées.SetDataBinding(dataSet1, "Clients");
      }

      private void buttonUpdate_Click(object sender, System.EventArgs e)
	  {
         da.Update(dataSet1, "Clients");
         MessageBox.Show("Modification enregistrée");
      }

    }

}

Leave a Reply

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