Xceed Grid for WinForms v4.3 Documentation
Data binding

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Providing Data > Data binding

In order to bind the grid to a data source, the DataSource and optionally the DataMember properties must be set or the SetDataBinding method called. If another control is bound to the same data source as the grid, then both the DataSource and DataMember properties must be set in order for the other control to be synchronized with the grid. 

The following table provides a listing of the supported data sources: 

DataSource Description
DataTable Represents one table of in-memory data.
DataView Represents a databindable, customized view of a DataTable for sortind, filtering, searching, editing, and navigation.
DataSet Represents an in-memory cache of data.
DataViewManager Contains a default DataViewSettingCollection for each DataTable in a DataSet.
Any component that implements the IListSource interface Provides functionality to an object to return a list that can be bound to a data source.
Any component that implements the IList interface Represents a collection of objects that can be individually accessed by index.
Jagged arrays An array whose elements are arrays.

Demonstrations

The following example demonstrates how to bind the grid to a dataset filled by an ADO data adapter:

VB.NET Copy Code
Imports Xceed.Grid
Imports System.Data.OleDb

Dim connectionString As String = "" 'connection query
Dim connection As New OleDbConnection( connectionString )

connection.Open()

Dim selectQuery As String = "SELECT * FROM Clients"
Dim dataAdapter As New OleDbDataAdapter( selectQuery, connection )
Dim dataSet As New DataSet( "Clients" )          

dataAdapter.Fill( dataSet )

gridControl1.DataSource = dataSet
gridControl1.DataMember = "Clients"                                      
C# Copy Code
using Xceed.Grid;
using System.Data.OleDb;

string connectionString = ""; //connection query
OleDbConnection connection = new OleDbConnection( connectionString );

connection.Open();

string selectQuery = "SELECT * FROM Clients";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter( selectQuery, connection );
DataSet dataSet = new DataSet( "Clients" );

dataAdapter.Fill( dataSet );

gridControl1.DataSource = dataSet;
gridControl1.DataMember = "Clients";

The next example demonstrates how to bind the grid to a jagged array:

VB.NET Copy Code

' Create a jagged array
Dim data()() As Object=

New Object( 9 )() {
New Object( 3 ) { "Canada" , 31500000 , "Ottawa" , "12.2 ºc" },
New Object( 3 ) { "Switzerland" , 7300000, "Bern"   , "23.3 ºc"},
New Object( 3 ) { "France" , 59500000 , "Paris" , "27.3 ºc" } ,
New Object( 3 ) { "USA" , 278000000 , "Washington" , "14.1 ºc" } ,
New Object( 3 ) { "UK" , 59700000, "London" , "23.7 ºc" } ,
New Object( 3 ) { "Belgium" , 10300000, "Brussels" , "21.8 ºc" } ,
New Object( 3 ) { "Italy" , 57700000 , "Rome" , "29.6 ºc"} ,
New Object( 3 ) { "Spain" , 40000000 , "Madrid" , "31.8 ºc" } ,
New Object( 3 ) { "Germany" , 83000000, "Berlin" , "25.1 ºc" } ,
New Object( 3 ) { "Japan" , 126800000, "Tokyo" , "17.2 ºc" } }
                                             }
' Assign the jagged array to the DataSource property
gridControl1.DataSource = data   

' Arrange and format the titles of the columns.
Dim nfi As New NumberFormatInfo()
nfi.NumberGroupSeparator = " "

gridControl1.Columns( 1 ).FormatProvider = nfi
gridControl1.Columns( 1 ).FormatSpecifier = "n0";

gridControl1.Columns( 0 ).Title = "Country"
gridControl1.Columns( 1 ).Title = "Population"
gridControl1.Columns( 2 ).Title = "Capital"
gridControl1.Columns( 3 ).Title = "Average Temperature"
C# Copy Code
using Xceed.Grid;
  
using System.Globalization;

// Create a jagged array
object[][] data= new object[ 10 ][] {
new object[ 4 ] { "Canada" , 31500000 , "Ottawa" , "12.2 ºc" },
new object[ 4 ] { "Switzerland" , 7300000, "Bern"   , "23.3 ºc"},
new object[ 4 ] { "France" , 59500000 , "Paris" , "27.3 ºc" } ,
new object[ 4 ] { "USA" , 278000000 , "Washington" , "14.1 ºc" } ,
new object[ 4 ] { "UK" , 59700000, "London" , "23.7 ºc" } ,
new object[ 4 ] { "Belgium" , 10300000, "Brussels" , "21.8 ºc" } ,
new object[ 4 ] { "Italy" , 57700000 , "Rome" , "29.6 ºc"} ,
new object[ 4 ] { "Spain" , 40000000 , "Madrid" , "31.8 ºc" } ,
new object[ 4 ] { "Germany" , 83000000, "Berlin" , "25.1 ºc" } ,
     new object[ 4 ] { "Japan" , 126800000, "Tokyo" , "17.2 ºc" } };     

            
// Assign the jagged array to the DataSource property                           
gridControl1.DataSource = data;

// Arrange and format the titles of the columns.
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = " ";

gridControl1.Columns[1].FormatProvider = nfi ;
gridControl1.Columns[1].FormatSpecifier = "n0" ;

gridControl1.Columns[0].Title = "Country";
gridControl1.Columns[1].Title = "Population";
gridControl1.Columns[2].Title = "Capital";
gridControl1.Columns[3].Title = "Average Temperature";

If rows are added or removed from the jagged array from outside of the grid, then the jagged array must be reassigned to the DataSource property in order for the modifications to be reflected in the grid. 

If an existing value is changed in the jagged array from outside of the grid, for example the text of one of the elements, in order for the changes to be reflected in the grid, the jagged array can be reassigned to the grid OR the grid's UpdateRectangles method can be called.