Skip to main content

How to: Bind a Control to a Database at Runtime

This example demonstrates how to bind a data-aware control (XtraGrid, XtraPivotGrid, XtraVerticalGrid, etc.) to a database at runtime. Note that specific controls may need additional customization after the control is bound to a data source. For more information, refer to documentation of the corresponding control.

The following code demonstrates a way of binding a GridControl to the Products table of the NWind database.

using System.Data.OleDb;
// ...
// Create a connection object.
OleDbConnection connection = new OleDbConnection(
  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\DBs\\NWIND.MDB");

// Create a data adapter.
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Products", connection);

// Create and fill a dataset.
DataSet sourceDataSet = new DataSet();
adapter.Fill(sourceDataSet);

// Specify the data source for the grid control.
gridControl1.DataSource = sourceDataSet.Tables[0];