Database Reference
In-Depth Information
DataColumn col = dt.Columns[2];
A data row represents the data in a row. You can programmatically add, update, or delete rows in a
data table. To access rows in a data table, you use its Rows property, whose indexer accepts a zero-based
index, for example (where dt is a data table):
DataRow row = dt.Rows[2];
That's enough theory for now. It's time to do some coding and see how these objects work together
in practice!
Working with Data Sets and Data Adapters
The data set constructor is overloaded.
DataSet ds = new DataSet();
DataSet ds = new DataSet("MyDataSet");
If you use the parameterless constructor, the data set name defaults to NewDataSet . If you need more
than one data set, it's good practice to use the other constructor and name it explicitly. However, you
can always change the data set name by setting its DataSetName property.
You can populate a data set in several ways, including the following:
Using a data adapter
Reading from an XML document
In this chapter, we'll use data adapters. However, in the “Using Datasets and XML” section, you'll
take a quick peek at the converse of the second method, and you'll write from a data set to an XML
document.
Try It: Populating a DataSet with a Data Adapter
In this example, you'll create a data set, populate it with a data adapter, and then display its contents.
1. Create a new Windows Forms Application project named Chapter15. When
Solution Explorer opens, save the solution.
2. Rename the Chapter15 project to DataSetandDataAdapter.
3. Rename the Form1.cs file to Pop DataSet.cs .
4. Select the PopDataSet form by clicking the form's title bar, and set the Size
property's Width to 301 and Height to 342.
5. Drag a GridView control onto the form, and position it toward the top-left
corner of the form. Select this GridView, navigate to the Properties window,
and set the following properties:
Set the Name property to gvProduct.
For the Location property, set X to 12 and Y to 12.
Set the ScrollBars property to Both.
 
Search WWH ::




Custom Search