CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
Navigating Between Related Tables
Now let’s look at how a DataRelation allows you to move between related tables programmatically.
Extend your UI to include a new Button (named btnGetOrderInfo), a related TextBox (named txtCustID),
and a descriptive Label (you can group these controls within a GroupBox for more visual appeal).
Figure 22-12 shows one possible UI of the application.
Figure 22-12. The updated UI allows the user to look up customer order information
This updated UI lets the user enter the ID of a customer and retrieve all the relevant information
about that customer’s order (e.g., name, order ID, and car order). This information will be formatted into
a string type that is eventually displayed within a message box. Consider the following code behind the
new Button’s Click event handler:
private void btnGetOrderInfo_Click(object sender, System.EventArgs e)
{
string strOrderInfo = string.Empty;
DataRow[] drsCust = null;
DataRow[] drsOrder = null;
// Get the customer ID in the text box.
int custID = int.Parse(this.txtCustID.Text);
// Now based on custID, get the correct row in Customers table.
drsCust = autoLotDS.Tables["Customers"].Select(
string.Format("CustID = {0}", custID));
strOrderInfo += string.Format("Customer {0}: {1} {2}\n",
drsCust[0]["CustID"].ToString(),
drsCust[0]["FirstName"].ToString(),
drsCust[0]["LastName"].ToString());
// Navigate from Customers table to Orders table.
drsOrder = drsCust[0].GetChildRows(autoLotDS.Relations["CustomerOrder"]);
900