DataGridView Binding - OLEDB dataset

The DataGridView provides the capability to exhibit data through Bound mode, unbound mode, and Virtual mode. For beginners, investigating into basic data-binding scenarios proves to be the most straightforward way to use the DataGridView control's functionality.

How to bind an OLEDB dataset

When a data source is specified for the DataGridView, it conveniently generates columns automatically, tailored to the data types within the source, making the setup process seamless and effortless. By intelligently analyzing the data source, the DataGridView constructs an appropriate column structure, eliminating the need for manual column creation.

The following C# program shows how to bind an OLEDB dataset to a DataGridView.

Full Source C#
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"; } } }