You can use the SetDataBinding method to bind the grid at run time. For example, the following code binds the C1TrueDBGrid control to the Customers table in the DSCustomers data source:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Me.C1TrueDBGrid1.SetDataBinding(Me.DsCustomers.Customers, "") |
To write code in C#
C# |
Copy Code
|
---|---|
this.c1TrueDBGrid1.SetDataBinding(this.DsCustomers.Customers, ""); |
If the DataSource is reset through code, it will show all of the data in the grid and will not keep the initial layout created with the Designer. You can ensure that the grid layout remains as designed by using the SetDataBinding method with the HoldFields parameter set to True. For example
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
C1TrueDBGrid1.SetDataBinding(Me.DsCustomers.Customers, "", True) |
To write code in C#
C# |
Copy Code
|
---|---|
this.c1TrueDBGrid1.SetDataBinding(this.DsCustomers.Customers, "", true); |
For another example of using the SetDataBinding(Object, String, Boolean) method, see Tutorial 2: Using True DBGrid for WinForms with SQL Query Results.
Note that you can create an unbound grid by using the SetDataBinding method without arguments. See Creating an Unbound Grid for details.