ComponentOne List for WinForms
In This Topic
    Tutorial 19 – Using the LookUp Feature
    In This Topic

    In this tutorial, you will learn how to use the LookUp feature to change the value of a C1Combo box.

    In the database C1NWind.mdb there are two tables: CustType and Customers. CustType has two fields: TypeDesc and TypeId. Customers has a field called CustType corresponding to the TypeId in the CustType table. In other words, the CustType field in the Customers table is the foreign key to the CustType table. When users want to navigate or update the Customers table, they usually want more meaningful words than Ids. In our case, they want the TypeDesc instead of the TypeId. This is where the LookUp feature comes into play.

    1. Create a new .NET project.
    2. Double-click the C1List icon in Visual Studio's Toolbox to add it to the Form. 
      Note: See Adding the C1List Components to a Project for information on adding a component to the Toolbox.
    3. Add a C1Combo control, a DataGrid object, and a button control to the form. Change the button's Text property to "Update to DataBase".
    4. Go to the DataSource property for C1List and select Add Project Data Source from the drop-down. In the adapter's Data Source Configuration Wizard, either select a connection to C1NWind.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select all fields in the Customers table and all fields in the CustType table, and type "DataSet1" into the DataSet name box, and then finish out the wizard.
    5. Select DataGrid1 and set its DataSource property to DataSet1.Customers in the Properties panel. Select C1Combo1 and set its DataSource property to DataSet1.CustType. Set its ValueMember property to TypeId.
    6. Add the following code in the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' The following two lines are added by Visual Studio 2005.    
      Me.CustomersTableAdapter.Fill(Me.DataSet1.Customers)    
      Me.CustTypeTableAdapter.Fill(Me.DataSet1.CustType)
      
      ' Add a relation.    
      Dim parentCol, childCol As DataColumn    
      Dim relCustType As DataRelation    
      parentCol = DataSet1.Tables("CustType").Columns("TypeId")    
      childCol = DataSet1.Tables("Customers").Columns("CustType")    
      relCustType = New DataRelation("CustomersType", parentCol, childCol)    
      DataSet1.Relations.Add(relCustType)
      
      ' Bind the SelectedValue property.    
      Me.C1Combo1.DataBindings.Add("SelectedValue", DataSet1.Tables("Customers"), "CustType")
      

      To write code in C#

      C#
      Copy Code
      // The following two lines are added by Visual Studio 2005.    
      this.CustomersTableAdapter.Fill(this.DataSet1.Customers);   
      this.CustTypeTableAdapter.Fill(this.DataSet1.CustType);
       
      // Add a relation.    
      DataColumn parentCol, childCol;    
      DataRelation relCustType;   
      parentCol = DataSet1.Tables["CustType"].Columns["TypeId"];    
      childCol = DataSet1.Tables["Customers"].Columns["CustType"];    
      relCustType = new DataRelation("CustomersType", parentCol, childCol);    
      DataSet1.Relations.Add(relCustType);
      
      // Bind the SelectedValue property.    
      this.c1Combo1.DataBindings.Add("SelectedValue", DataSet1.Tables["Customers"], "CustType");
      
    7. Add the following code to the Button1_Click event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.CustomersTableAdapter.Connection.Open()    
      Dim ds As DataSet    
      ds = DataSet1.GetChanges(DataRowState.Modified)
      
      If (Not ds Is Nothing) Then    
          Try    
              Me.CustomersTableAdapter.Update(ds)    
              Me.CustTypeTableAdapter.Update(ds)    
          Catch eU As Exception   
              MessageBox.Show(eU.Message)    
          End Try    
      End If
      
      Me.CustomersTableAdapter.Connection.Close()
      

      To write code in C#

      C#
      Copy Code
      this.CustomersTableAdapter.Connection.Open();    
      DataSet ds;   
      ds = DataSet1.GetChanges(DataRowState.Modified);
        
      if (ds != null)    
      {    
          try    
          {    
              this.CustomersTableAdapter.Update(ds);    
              this.CustTypeTableAdapter.Update(ds);    
          }    
          catch (Exception eU)    
          {    
              MessageBox.Show(eU.Message);    
          }    
      }
      
      this.CustomersTableAdapter.Connection.Close();
      

    Run the program and observe the following:

    This concludes the tutorial.