Creating and Using a DataViewManager Object

Chia sẻ: Tuan Nghia | Ngày: | Loại File: PDF | Số trang:4

0
46
lượt xem
7
download

Creating and Using a DataViewManager Object

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Creating and Using a DataViewManager Object To create a DataViewManager, you use one of the following constructors: DataViewManager() DataViewManager

Chủ đề:
Lưu

Nội dung Text: Creating and Using a DataViewManager Object

  1. Creating and Using a DataViewManager Object To create a DataViewManager, you use one of the following constructors: DataViewManager() DataViewManager(DataSet myDataSet) where myDataSet specifies the DataSet used by the DataViewManager object. This sets the DataSet property of the new DataViewManager object to myDataSet. Let's take a look at an example of creating and using a DataViewManager. Assume you have a DataSet named myDataSet, which contains a DataTable populated with rows from the Customers table. The following example creates a DataViewManager object named myDVM, passing myDataSet to the constructor: DataViewManager myDVM = new DataViewManager(myDataSet); The next example sets the Sort and RowFilter properties that will be used later when a DataView for the Customers DataTable is created: myDVM.DataViewSettings["Customers"].Sort = "CustomerID"; myDVM.DataViewSettings["Customers"].RowFilter = "Country = 'UK'"; Note The previous code doesn't actually create a DataView; it merely sets the properties of any DataView created in the future that views rows from the Customers DataTable. The following example actually creates a DataView by calling the CreateDataView() method of the myDVM DataViewManager, passing the customersDT DataTable to CreateDataView(): DataView customersDV = myDVM.CreateDataView(customersDT); The Sort and RowFilter properties of the customersDV DataView are set to CustomerID and Country = 'UK' respectively. These are the same settings as those set earlier in the DataViewSettings property. Listing 13.4A shows a complete example that creates and uses the DataViewManager shown in this section. Listing 13.4A: USINGDATAVIEWMANAGER.CS /*
  2. UsingDataViewManager.cs illustrates the use of a DataViewManager object */ using System; using System.Data; using System.Data.SqlClient; class UsingDataViewManager { public static void Main() { SqlConnection mySqlConnection = new SqlConnection( "server=localhost;database=Northwind;uid=sa;pwd=sa" ); SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = "SELECT CustomerID, CompanyName, Country " + "FROM Customers"; SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); mySqlDataAdapter.SelectCommand = mySqlCommand; DataSet myDataSet = new DataSet(); mySqlConnection.Open(); mySqlDataAdapter.Fill(myDataSet, "Customers"); mySqlConnection.Close(); DataTable customersDT = myDataSet.Tables["Customers"]; // create a DataViewManager object named myDVM DataViewManager myDVM = new DataViewManager(myDataSet); // set the Sort and RowFilter properties for the Customers DataTable myDVM.DataViewSettings["Customers"].Sort = "CustomerID"; myDVM.DataViewSettings["Customers"].RowFilter = "Country = 'UK'"; // display the DataViewSettingCollectionString property of myDVM Console.WriteLine("myDVM.DataViewSettingCollectionString = " + myDVM.DataViewSettingCollectionString + "\n"); // call the CreateDataView() method of myDVM to create a DataView // named customersDV for the customersDT DataTable DataView customersDV = myDVM.CreateDataView(customersDT);
  3. // display the rows in the customersDV DataView object foreach (DataRowView myDataRowView in customersDV) { for (int count = 0; count < customersDV.Table.Columns.Count; count++) { Console.WriteLine(myDataRowView[count]); } Console.WriteLine(""); } } } The output from this program is as follows: myDVM.DataViewSettingCollectionString = AROUT Around the Horn UK BSBEV B's Beverages UK CONSH Consolidated Holdings UK EASTC Eastern Connection UK ISLAT Island Trading UK NORTS North/South UK
  4. SEVES Seven Seas Imports UK
Đồng bộ tài khoản