HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

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.

C#:

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)

 

No comments:

Post a Comment