Free mag vol1 | Page 922

CHAPTER 22  ADO.NET PART II: THE DISCONNECTED LAYER Instead, you obtain a new DataRow object from a given DataTable. For example, assume you want to insert two rows in the Inventory table. The DataTable.NewRow() method allows you to obtain the next slot in the table, at which point you can fill each column with new data using the type indexer. When doing so, you can specify either the string name assigned to the DataColumn or its (zero-based) ordinal position. static void FillDataSet(DataSet ds) { ... // Now add some rows to the Inventory table. DataRow carRow = inventoryTable.NewRow(); carRow["Make"] = "BMW"; carRow["Color"] = "Black"; carRow["PetName"] = "Hamlet"; inventoryTable.Rows.Add(carRow); } carRow = inventoryTable.NewRow(); // Column 0 is the autoincremented ID field, // so start at 1. carRow[1] = "Saab"; carRow[2] = "Red"; carRow[3] = "Sea Breeze"; inventoryTable.Rows.Add(carRow);  Note If you pass the DataRow’s indexer method an invalid column name or ordinal position, you will receive a runtime exception. At this point, you have a single DataTable containing two rows. Of course, you can repeat this general process to create a number of DataTables to define the schema and data content. Before you insert the inventoryTable object into your DataSet object, you should check out the all-important RowState property. Understanding the RowState Property The RowState property is useful when you need to identify programmatically the set of all rows in a table that have changed from their original value, have been newly inserted, and so forth. You can assign this property any value from the DataRowState enumeration, as shown in Table 22-5. Table 22-5. Values of the DataRowState Enumeration 868 Value Meaning in Life Added The row has been added to a DataRowCollection, and AcceptChanges() has not been called.