Creating andUsingaDataViewManagerObject
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 DataViewManagerobject to myDataSet.
Let's take a look at an example of creating andusinga DataViewManager. Assume you
have a DataSet named myDataSet, which contains a DataTable populated with rows from
the Customers table. The following example creates aDataViewManagerobject 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'";
N
ote 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
D
ataTable.
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
/*
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 aDataViewManagerobject 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);
// 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
SEVES
Seven Seas Imports
UK
.
Creating and Using a DataViewManager Object
To create a DataViewManager, you use one of the following constructors:
DataViewManager( )
DataViewManager( DataSet. 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