ComponentOne DataObjects for .NET
Tutorial 5: Customizing Data Behavior with Events
DataObjects for .NET Express Edition > DataObjects for .NET Express Tutorials > Tutorial 5: Customizing Data Behavior with Events

In this tutorial, you will learn how to use C1ExpressTable events to customize data behavior in code.

Create a new .NET 2.0 Windows Application project.

  1. Place the following components on the form as shown in the image below:
    Number of Components Name Namespace
    1 C1ExpressTable C1ExpressTable1 C1.Data.Express.C1ExpressTable
    1 C1TrueDBGrid C1TrueDBGrid1 C1.Win.C1TrueDBGrid.C1TrueDBGrid

  2. Select the C1ExpressTable1 component.
  3. Click the drop-down arrow next to the ConnectionString property in the Properties window and select <New Connection…>. The Add Connection dialog box opens.
  4. 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.
  5. Click the drop-down arrow next to the DbTableName property and select Order Details from the database table list. Click Yes to retrieve the fields.
  6. Select the C1TrueDBGrid1and set its DataSource property to C1ExpressTable1 in the Properties window.
  7. Add the following event handlers, C1ExpressTable1_BeforeFieldChange and C1ExpressTable1_BeforeEndEdit, and their code for C1ExpressTable1:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1ExpressTable1_BeforeFieldChange _
    (ByVal sender As Object, ByVal e As  C1.Data.FieldChangeEventArgs) Handles _
    C1ExpressTable1.BeforeFieldChange
    If e.Field.Name = "Quantity" Then
      If e.NewValue < 1 Then
              e.NewValue = 1
              End If
    ElseIf e.Field.Name = "Discount" Then
      If e.NewValue < 0 Or e.NewValue > 1 Then
              Throw New ApplicationException("Discount must be between 0 and 1")
      End If
    End If
    End Sub
     
    Private Sub C1ExpressTable1_BeforeEndEdit _
    (ByVal sender As Object, ByVal e As C1.Data.RowChangeEventArgs) Handles _
    C1ExpressTable1.BeforeEndEdit
    If e.Row("Quantity") * e.Row("UnitPrice") > 100000 Then
      Throw New ApplicationException("Too expensive")
    End If
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void c1ExpressTable1_BeforeFieldChange
    (object sender, C1.Data.FieldChangeEventArgs e)
    {
    if (e.Field.Name == "Quantity")
    {
    if ((short)e.NewValue < 1)
    e.NewValue = (short)1;
    }
    else if (e.Field.Name == "Discount")
    {
    if ((float)e.NewValue < 0 || (float)e.NewValue > 1)
    throw new ApplicationException("Discount must be between 0 and 1");
    }
    }
     
    private void c1ExpressTable1_BeforeEndEdit
    (object sender, C1.Data.RowChangeEventArgs e)
    {
    if ((short)e.Row["Quantity"] * (decimal)e.Row["UnitPrice"] > 100000)
    throw new ApplicationException("Too expensive");
    }
    

Run the program and observe the following: