Back to Devexpress

How to: Bind a Control to a Database at Runtime

windowsforms-2240-common-features-data-binding-how-to-bind-a-control-to-a-database-at-runtime.md

latest1.6 KB
Original Source

How to: Bind a Control to a Database at Runtime

  • Oct 29, 2020

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.

csharp
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];
vb
Imports System.Data.OleDb
' ...
' Create a connection object.
Dim Connection As New OleDbConnection( _
  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\DBs\\NWIND.MDB")

' Create a data adapter.
Dim Adapter As New OleDbDataAdapter("SELECT * FROM Products", Connection)

' Create and fill a dataset.
Dim SourceDataSet As New DataSet()
Adapter.Fill(SourceDataSet)

' Specify the data source for the grid control.
GridControl1.DataSource = SourceDataSet.Tables(0)