Nội dung Text: Creating a DataView Using Visual Studio .NET
Creating a DataView Using Visual Studio .NET
In this section, you'll learn how to create a DataView using Visual Studio .NET (VS
.NET). You can follow along with the steps described in this section:
1. Open VS .NET and create a new Windows application named myDataView.
2. Display Server Explorer, connect to your Northwind database, and drag the
Customers table to your form. This creates a SqlConnection object named
sqlConnection1 and a SqlDataAdapter object named sqlDataAdapter1. These
objects are placed in the tray beneath your form.
3. Alter the ConnectionString property of sqlConnection1 to connect to your
Northwind database. Remember to add a substring containing the password
(pwd=sa, or similar).
4. Click on the sqlDataAdapter1 object in your form, and then click the Generate
Dataset link at the bottom of the Properties window for sqlDataAdapter1. Accept
the defaults in the dialog box, and click the OK button to create a DataSet object
5. Drag a DataView object from the Data tab of the Toolbox to your form. This
creates a DataView object named dataView1.
6. Set the Table property of your dataView1 object to dataSet11.Customers using the
drop-down list to the right of the Table property; set the RowFilter property to
Country='UK'; and set the Sort property to CustomerID. See Figure 13.1.
Figure 13.1: Setting the Properties of dataView1
7. Drag a DataGrid control from the Windows Forms tab of the Toolbox to your
form. This creates a DataGrid object named dataGrid1.
8. Set the DataSource property of dataGrid1 to dataView1 using the drop-down list
to the right of the DataSource property, as shown in Figure 13.2. This binds the
data stored in dataView1 to dataGrid1 and allows dataGrid1 to access any data
stored in dataView1.
Figure 13.2: Setting the Properties of dataGrid1
9. Select View ➣ Code and set the Form1() method of your form to
10. public Form1()
13. // Required for Windows Form Designer support
17. // call the Fill() method of sqlDataAdapter1
18. // to populate dataSet11 with a DataTable named
19. // Customers
20. sqlDataAdapter1.Fill(dataSet11, "Customers");
Compile and run your form by pressing Ctrl+F5. Figure 13.3 shows the running form.
Notice that the information in the form comes from the DataView you created.