Advertise with us

DataGridView Binding - OLEDB dataset in C#

Hi MindStickians,
How to bind datagridview with OLEDB dataset in C#?
Last updated:6/15/2013 7:42:57 AM

1 Answers

Sumit Kesarwani
Sumit Kesarwani

Hi Samuel,


You can use this code:

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=your .mdb file;";
            string sql = "SELECT * FROM Authors";
            OleDbConnection connection = new OleDbConnection(connetionString);
            OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
            DataSet ds = new DataSet();
            connection.Open();
            dataadapter.Fill(ds, "Authors_table");
            connection.Close();
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "Authors_table";
        }
    }
}

Last updated:6/15/2013 7:42:57 AM

Answer