3.3 Locate Records with the DataTable Object
Using the DataTable object, you can use another object called the DataRow object that allows you to locate a specific row in the data table. This is useful when you want to present your users with a search mechanism for your form. This How-To shows you how to locate a specific row within your data table and how to use the same data table for two different purposes.
After you have your DataTable object loaded in memory, you want to be able to locate specific records within the DataTable object. How do you locate records in the DataTable object?
Technique
For this How-To, you are going to use a ComboBox control instead of a ListBox control. You will use the same technique for loading the combo box as you would the list box. The change comes when you select an item from the combo box.
When an item is selected in the combo box, the SelectedIndexChanged event is fired off. Within this event, you will take the combo box's SelectedItem, which gives the ValueMember that is located in the selected row, and use that with the Find method off the DataTables Rows collection.
With the data row located, the corresponding columns are loaded into text boxes on the form, as shown in Figure 3.4.
Figure 3.4 This combo box will point the user to a specific customer.
Steps
Open and run the VB.NETChapter 3 solution. From the main form, click on the command button with the caption How-To 3.3. When the form loads, pick a new customer from the list that is presented in the customer ComboBox control. You will see the text boxes below the ComboBox control display new data that corresponds to the chosen customer.
Create a new Windows Form.
Add some labels, combo boxes, and text boxes, as listed in Table 3.4.
Table 3.4 Label, TextBox, and ComboBox Control Property Settings
Object |
Property |
Setting |
Label |
Name |
Label1 |
Caption |
Customer |
|
ComboBox |
Name |
cboCustomers |
Label |
Name |
Label2 |
Caption |
Customer ID |
|
Label |
Name |
Label3 |
Caption |
Company Name |
|
Label |
Name |
Label4 |
Caption |
Address |
|
Label |
Name |
Label5 |
Caption |
City |
|
TextBox |
Name |
txtCustomerID |
TextBox |
Name |
txtCompanyName |
TextBox |
Name |
txtAddress |
TextBox |
Name |
txtCity |
You will also want to make sure that the Text properties in the TextBox controls are blank.
-
In the class module for the form, add the following two Private declarations just below the line of code that reads Windows Form Designer generated code.
Private modaCust As OleDb.OleDbDataAdapter Private mdtCust As DataTable = New DataTable()
These lines of code declare a data adapter and a data table that will be used throughout the form.
NOTE
Adding the m on the front tells you that it is a module- or member-level variable.
Also, remember that although you are declaring this at the form level, the connection that is used for the data adapter is not going to be left open the whole time the form is. When the data table is filled, the connection is opened. Then the data is accessed locally using XML under the covers. It is disconnected from the server.
- Add the code shown in Listing 3.4 to the Load event of the form. Almost identical to the code in the last How-To to load a ListBox control, this code sets modaCust to a SQL String and the connection string to be used. mdtCust is then filled using the Fill method of modaCust. Next, the first element in the DataColumn array called dc is set to the CustomerID column. mdtCustPrimaryKey is then set to the DataColumn array. Last, the DataSource, DisplayMember, and ValueMember properties are set.
Listing 3.4 frmHowTo3_3.vb: Loading a ComboBox by Using the DataTable Object
Private Sub frmHowTo3_3_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim strSQL As String Dim dc(1) As DataColumn '-- Set up the exception catch Try '-- Create the data adapter and fill the data table modaCust = New _ OleDb.OleDbDataAdapter("Select * From Customers", _ (BuildCnnStr("(local)", "Northwind"))) modaCust.Fill(mdtCust) '-- Set up the primary key for the data table dc(0) = mdtCust.Columns("CustomerID") mdtCust.PrimaryKey = dc '-- Bind the data to the combo box cboCustomers.DataSource = mdtCust cboCustomers.DisplayMember = "CompanyName" cboCustomers.ValueMember = "CustomerID" Catch oexpData As OleDb.OleDbException MsgBox(oexpData.Message) End Try End Sub
The PrimaryKey property that was set will be used in the code for the next step by the Find method of mdtCust's Rows collection.
-
This last bit of code needs to be added to the SelectedIndexChanged event of the cboCustomers ComboBox control. As with the last step, when a data column was set up for the PrimaryKey property, in this step an array is specified to pass the SelectedItem value to find the Find method. The text boxes' Text properties are then set to the column values by using the ToString method.
Listing 3.5 frmHowTo3_3.vb: Locating a Record in the Data Table, and Then Assigning Values to Text Boxes
Private Sub cboCustomers_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cboCustomers.SelectedIndexChanged Dim drCurr As DataRow Dim aFindValue(0) As Object '-- Load the item to look up, and use the find method aFindValue(0) = cboCustomers.SelectedItem(0) drCurr = mdtCust.Rows.Find(aFindValue) '-- Load up the fields on the form txtCustomerID.Text = drCurr("CustomerID").ToString txtCompanyName.Text = drCurr("CompanyName").ToString txtAddress.Text = drCurr("Address").ToString txtCity.Text = drCurr("City").ToString End Sub
How It Works
When a user picks a customer from the cboCustomer ComboBox control, the code then locates the desired value within the mdtCust data Table using the Find method off the rows collection. Text boxes are then loaded from the row that is retrieved.
Comments
Locating records within a data table and data row is pretty easy when you're using the methods that are supplied. ADO.NET provides the control you need, not only at the overall hierarchical level, but also at the row and column levels.