ComponentOne DataObjects for .NET
Tutorial 1: Binding to a Simple Table
DataObjects for .NET Express Edition > DataObjects for .NET Express Tutorials > Tutorial 1: Binding to a Simple Table

In this tutorial, you will see how easy it is to bind to simple database table data using DataObjects for .NET Express Edition. Complete the following steps:

  1. Create a new .NET 2.0 Windows Application project.1. Place the following components on the form as shown in the figure.
    Number of Components Name Namespace
    1 C1ExpressTable C1ExpressTable1 C1.Data.Express.C1ExpressTable
    1 DataGridView DataGridView1 System.Windows.Forms.DataGridView
    1 Label Label1 System.Windows.Forms.Label
    1 CheckBox CheckBox1 System.Windows.Forms.CheckBox
    1 command Button Button1 System.Windows.Forms.Button


    In this tutorial we intentionally use Microsoft DataGridView, not a ComponentOne grid control. It is meant to show that DataObjects for .NET Express can serve as a data source to any data-bound GUI controls adhering to .NET data binding specification. For all other tutorials will use True DBGrid. Naturally, we recommend C1TrueDBGrid and other presentation controls, both because they are the best and because they are most closely integrated with DataObjects for .NET Express. Most DataObjects for .NET Express features, all features that rely only on standard .NET data binding, will work with any third-party data-bound control. However, one important feature, Virtual Mode, requires that you use a ComponentOne data bound grid (if you need a grid at all).
  2. Set the Text properties of the CheckBox and Button to the following:
    • CheckBox1.Text = UpdateLeavingRow
    • Label1.Text = Row count:
    • Button1.Text = Update
  3. Select the C1ExpressTable1 component.
  4. Click the drop-down arrow next to the ConnectionString property in the Properties window and select <New Connection…>. The Add Connection dialog box opens.
  5. Select the provider, the database and other necessary connection properties in that dialog box. In this tutorial, we use the standard MS Access Northwind sample database (C1NWind.mdb).
    1. Click the Change button, if necessary, and select Microsoft Access Database File. The .NET Framework Data Provider for OLE DB is selected under Data provider.
    2. Under Database file name, browse to locate the C1NWind.mdb. The database is installed in the Common folder of the ComponentOne Samples directory.
    3. Click OK to close the Add Connection dialog box.
  6. Click the drop-down arrow next to the DbTableName property and select Customers from the database table list. Click Yes to retrieve the fields.
  7. Select the DataGridView1 control, click the drop-down arrow next to the DataSource property in the Properties window and. select C1ExpressTable1. This binds the grid to the C1ExpressTable, and the Customers fields appear in the grid.

Run the program and observe the following:


Close the program and return to the design time environment. Next you'll access DataObjects for .NET Express data programmatically in code.

Continue by completing the following steps:

  1. Create an event handler, C1ExpressTable1_AfterFill, and enter the following code:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Label1.Text = "Row count: " + c1ExpressTable1.DataTable.Rows.Count.ToString()
    

    To write code in C#

    C#
    Copy Code
    label1.Text = "Row count: " + c1ExpressTable1.DataTable.Rows.Count.ToString();
    

    The AfterFill event occurs after the table is filled with data. The code shows the number of rows in the table in a label control.
  2. Create an event handler, Button1_Click, and enter the following code:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    C1ExpressTable1.ExpressConnection.Update()
    

    To write code in C#

    C#
    Copy Code
    c1ExpressTable1.ExpressConnection.Update();
    

    This code sends changes made by the end user to the database.
  3. Create an event handler, CheckBox1_CheckStateChanged, and enter the following code:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    C1ExpressTable1.UpdateLeavingRow = checkBox1.Checked
    

    To write code in C#

    C#
    Copy Code
    c1ExpressTable1.UpdateLeavingRow = checkBox1.Checked;
    

    This code toggles the UpdateLeavingRow property value when the check box is checked or unchecked.

Run the program and observe the following: