Free mag vol1 | Page 940

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER { } DataRow temp = properIDs[i]; strIDs += temp["PetName"] + " is ID " + temp["ID"] + "\n"; } MessageBox.Show(strIDs, "Pet names of cars where ID > 5"); Updating Rows Within a DataTable The final aspect of the DataTable you should be aware of is the process of updating an existing row with new values. One approach is to first obtain the row(s) that match a given filter criterion using the Select() method. Once you obtain the DataRow(s) in question, modify them accordingly. For example, assume you have a new Button on your form named btnChangeMakes that (when clicked) searches the DataTable for all rows where Make is equal to BMW. Once you identify these items, you change the Make from BMW to Yugo, as so: // Find the rows you want to edit with a filter. private void btnChan geMakes_Click(object sender, EventArgs e) { // Confirm selection. if (DialogResult.Yes == MessageBox.Show("Are you sure?? BMWs are much nicer than Yugos!", "Please Confirm!", MessageBoxButtons.YesNo)) { // Build a filter. string filterStr = "Make='BMW'"; string strMake = string.Empty; // Find all rows matching the filter. DataRow[] makes = inventoryTable.Select(filterStr); } // Change all Beemers to Yugos! for (int i = 0; i < makes.Length; i++) { makes[i]["Make"] = "Yugo"; } } Working with the DataView Type A view object is an alternative representation of a table (or set of tables). For example, you can use Microsoft SQL Server to create a view for your Inventory table that returns a new table containing automobiles only of a given color. In ADO.NET, the DataView type allows you to extract a subset of data programmatically from the DataTable into a stand-alone object. One great advantage of holding multiple views of the same table is that you can bind these views to various GUI widgets (such as the DataGridView). For example, one DataGridView might be bound to a DataView showing all autos in the Inventory, while another might be configured to display only green automobiles. 886