Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Afficher les données de Microsoft Access dans un DataGrid

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

using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
 
public class DataGridMsAccess : System.Windows.Forms.Form {
 
    private System.Windows.Forms.DataGrid dataGridDonnées;
    private System.Windows.Forms.Button btnAfficher;
    public DataGridMsAccess() {
 
        Text = "Affichage de données Microsoft Access";
        this.dataGridDonnées = new System.Windows.Forms.DataGrid();
        this.btnAfficher = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridDonnées)).BeginInit();
        this.SuspendLayout();
 
        this.dataGridDonnées.DataMember = "";
        this.dataGridDonnées.HeaderForeColor = System.Drawing.SystemColors.ControlText;
        this.dataGridDonnées.Location = new System.Drawing.Point(8, 8);
        this.dataGridDonnées.Size = new System.Drawing.Size(280, 224);
 
        this.btnAfficher.Location = new System.Drawing.Point(120, 240);
        this.btnAfficher.Size = new System.Drawing.Size(152, 32);
        this.btnAfficher.Text = "Afficher les Informations";
        this.btnAfficher.Click += new System.EventHandler(this.btnAfficher_Click);
 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.btnAfficher,
                                                                          this.dataGridDonnées});
        ((System.ComponentModel.ISupportInitialize)(this.dataGridDonnées)).EndInit();
        this.ResumeLayout(false);
 
    }
    [STAThread]
    static void Main() {
        Application.Run(new DataGridMsAccess());
    }
 
    private OdbcConnection cn;
 
    private void btnAfficher_Click(object sender, System.EventArgs e) {
        try {
            conn= new OdbcConnection(@"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=.Clients.mdb");
            conn.Open();
            OdbcCommand commandeOld = new OdbcCommand("SELECT * from Clients", conn);
            OdbcDataAdapter da = new OdbcDataAdapter(commandeOld);
            DataSet ds = new DataSet("ID");
            da.Fill(ds);
            dataGridDonnées.DataSource = ds.Tables[0];
        } catch (Exception ex) {
            MessageBox.Show(@"Exception:"+ex);
            Debug.WriteLine(ex.ToString());
        } finally {
            cn.Close();
        }
    }
 
    private void InitializeComponent()
    {
        this.SuspendLayout();
        //
        // DataGridMsAccess
        //
        this.ClientSize = new System.Drawing.Size(295, 268);
        this.Name = "DataGridMsAccess";
        this.ResumeLayout(false);
 
    }
 
    public OdbcConnection conn { get; set; }
}

No comments yet.

Leave a Comment