Binding a DataSet to a Windows Forms DataGrid

364 ShippedDate1997-12-24T00:00:00.0000000-06:00ShippedDate ShipVia3ShipVia Freight1.51Freight ShipNameGROSELLA-RestauranteShipName ShipAddress5th Ave. Los Palos GrandesShipAddress ShipCityCaracasShipCity ShipRegionDFShipRegion ShipPostalCode1081ShipPostalCode ShipCountryVenezuelaShipCountry Orders MyDataSetName The syntax of this overloaded version of the WriteXml function is: Public Overloads Sub WriteXmlByVal fileName As String The fileName parameter specifies the full path of a file into which to write the XML. The XML document written by the DataSet classs WriteXml method can be read back into a DataSet object using the ReadXml method. Ex am ple 8- 7 reads back the file written by the code in Ex am ple 8- 5 . Example 8-7. Recreating a DataSet object from XML Dim ds As New DataSet ds.ReadXmlc:\temp.xml The XML created by the WriteXml method contains only data—no schema information. The ReadXml method is able to infer the schema from the data. To explicitly write the schema information, use the WriteXmlSchema method. To read the schema back in, use the ReadXmlSchema method. The GetXml and GetXmlSchema methods work the same as the WriteXml and WriteXmlSchema methods, except that each returns its result as a string rather than writing it to a file.

8.8 Binding a DataSet to a Windows Forms DataGrid

DataSet and DataTable objects can be bound to Windows Forms DataGrid objects to provide an easy way to view data. This is done by calling a DataGrid objects SetDataBinding method, passing the object that is to be bound to the grid. The syntax of the SetDataBinding method is: Public Sub SetDataBinding _ ByVal dataSource As Object, _ ByVal dataMember As String _ The parameters are: dataSource The source of the data to show in the grid. This can be any object that exposes the System.Collections.IList or System.Data.IListSource interfaces, which includes the DataTable and DataSet classes discussed in this chapter. dataMember If the object passed in the dataSource parameter contains multiple tables, as a DataSet object does, the dataMember parameter identifies the table to display in the DataGrid. If a 365 DataTable is passed in the dataSource parameter, the dataMember parameter should contain either Nothing or an empty string. Ex am ple 8- 8 shows how to bind a DataSource object to a DataGrid. The DataSource object contains a Customers table and an Orders table, and a relation between them. The call to the DataGrid objects SetDataBinding method specifies that the Customers table should be shown in the grid. Figur e 8- 3 shows the resulting DataGrid display. Example 8-8. Creating a DataSet and binding it to a Windows Forms DataGrid Open a database connection. Dim strConnection As String = _ Data Source=localhost;Initial Catalog=Northwind; _ Integrated Security=True Dim cn As SqlConnection = New SqlConnectionstrConnection cn.Open Set up a data adapter object. Dim strSql As String = _ SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers _ WHERE City = Buenos Aires AND Country = Argentina Dim da As SqlDataAdapter = New SqlDataAdapterstrSql, cn Load a data set. Dim ds As DataSet = New DataSet da.Fillds, Customers Set up a new data adapter object. strSql = _ SELECT Orders.OrderID, Orders.CustomerID, Orders.OrderDate, _ Orders.ShippedDate _ FROM Customers, Orders _ WHERE Customers.CustomerID = Orders.CustomerID _ AND Customers.City = Buenos Aires _ AND Customers.Country = Argentina da = New SqlDataAdapterstrSql, cn Load the data set. da.Fillds, Orders Close the database connection. cn.Close Create a relation. ds.Relations.AddCustomerOrders, _ ds.TablesCustomers.ColumnsCustomerID, _ ds.TablesOrders.ColumnsCustomerID Bind the data set to a grid. Assumes that grid contains a reference to a System.WinForms.DataGrid object. grd.SetDataBindingds, Customers Figure 8-3. The display generated by the code in Ex a m ple 8 - 8 366 Note in Figur e 8- 3 that each row in this DataGrid has a + icon. The reason is that the DataGrid object has detected the relation between the Customers table and the Orders table. Clicking on the + reveals all of the relations for which the Customers table is the parent. In this case, there is only one, as shown in Figur e 8 - 4 . Figure 8-4. Clicking the + reveals relations The name of the relation in the display is a link. Clicking on this link loads the grid with the child table in the relation, as shown in Figur e 8- 5 . Figure 8-5. The Orders table 367 While the child table is displayed, the corresponding row from the parent table is displayed in a header shown in Figur e 8- 5 . To return to the parent table, click the left-pointing triangle in the upper-right corner of the grid.

8.9 Binding a DataSet to a Web Forms DataGrid