List for WinForms Tutorials > Tutorial 19 Using the LookUp Feature |
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.
Note: See Adding the C1List Components to a Project for information on adding a component to the Toolbox. |
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"); |
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(); |
Note: When C1Combo has the focus, press F4 to open the drop-down box. |
This concludes the tutorial.