' Create a Connection, DataAdapters, and a DataSet.
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
Dim orderDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID, CustomerID FROM Orders", nwindConn)
Dim ordDetDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID, ProductID, Quantity FROM [Order Details]", nwindConn)
Dim custDS As DataSet = New DataSet()
' Open the Connection.
nwindConn.Open()
' Fill the DataSet with schema information and data.
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
orderDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
ordDetDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
custDA.Fill(custDS, "Customers")
orderDA.Fill(custDS, "Orders")
ordDetDA.Fill(custDS, "OrderDetails")
' Close the Connection.
nwindConn.Close()
' Create relationships.
custDS.Relations.Add("CustomerOrders", _
custDS.Tables("Customers").Columns("CustomerID"), _
custDS.Tables("Orders").Columns("CustomerID"))
custDS.Relations.Add("OrderDetails", _
custDS.Tables("Orders").Columns("OrderID"), _
custDS.Tables("OrderDetails").Columns("OrderID"))
' Create default DataView settings.
Dim myDVM As DataViewManager = New DataViewManager(custDS)
Dim myDVS As DataViewSetting
For Each myDVS In myDVM.DataViewSettings
myDVS.ApplyDefaultSort = True
Next
myDVM.DataViewSettings("Customers").Sort = "CompanyName"
' Bind to a DataGrid.
Dim myGrid As System.Windows.Forms.DataGrid = New System.Windows.Forms.DataGrid()
myGrid.SetDataBinding(myDVM, "Customers")
[C#]
// Create a Connection, DataAdapters, and a DataSet.
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
SqlDataAdapter orderDA = new SqlDataAdapter("SELECT OrderID, CustomerID FROM Orders", nwindConn);
SqlDataAdapter ordDetDA = new SqlDataAdapter("SELECT OrderID, ProductID, Quantity FROM [Order Details]", nwindConn);
DataSet custDS = new DataSet();
// Open the Connection.
nwindConn.Open();
// Fill the DataSet with schema information and data.
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
orderDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
ordDetDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
custDA.Fill(custDS, "Customers");
orderDA.Fill(custDS, "Orders");
ordDetDA.Fill(custDS, "OrderDetails");
// Close the Connection.
nwindConn.Close();
// Create relationships.
custDS.Relations.Add("CustomerOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]);
custDS.Relations.Add("OrderDetails",
custDS.Tables["Orders"].Columns["OrderID"],
custDS.Tables["OrderDetails"].Columns["OrderID"]);
// Create default DataView settings.
DataViewManager myDVM = new DataViewManager(custDS);
foreach (DataViewSetting myDVS in myDVM.DataViewSettings)
myDVS.ApplyDefaultSort = true;
myDVM.DataViewSettings["Customers"].Sort = "CompanyName";
// Bind to a DataGrid.
System.Windows.Forms.DataGrid myGrid = new System.Windows.Forms.DataGrid();
myGrid.SetDataBinding(myDVM, "Customers");