1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Creating and Using a DataViewManager Object pdf

4 351 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Creating and Using a DataViewManager Object
Thể loại Documentation
Định dạng
Số trang 4
Dung lượng 15,71 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Creating and Using a DataViewManager Object To create a DataViewManager, you use one of the following constructors: DataViewManager DataViewManagerDataSet myDataSet where myDataSet spec

Trang 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

shown in this section

Listing 13.4A: USINGDATAVIEWMANAGER.CS

/*

Trang 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);

Trang 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 =

<DataViewSettingCollectionString>

<Customers Sort="CustomerID" RowFilter="Country = 'UK'"

RowStateFilter="CurrentRows"/>

</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

Trang 4

SEVES

Seven Seas Imports

UK

Ngày đăng: 26/01/2014, 07:20