DekGenius.com
[ Team LiB ] Previous Section Next Section

28.2 Properties Reference

AllowDelete

Boolean allowDelete = DataView.AllowDelete;
DataView.AllowDelete = allowDelete;

Specifies whether it is possible to delete rows in the underlying DataTable using this DataView. It is the responsibility of the control or application to heed this setting; .NET doesn't enforce it in any way.

Example

The following example checks the AllowDelete property before removing a row:

DataView view = new DataView(ds.Tables["Customers"]);

// Select the first row.
DataRowView row = view[0];

// Delete it if allowed.
if (view.AllowDelete)
{
    row.Delete();
}
AllowEdit

Boolean allowEdit = DataView.AllowEdit;
DataView.AllowEdit = allowEdit;

Specifies whether it is possible to modify rows in the underlying DataTable using this DataView. It is the responsibility of the control or application to heed this setting; .NET doesn't enforce it in any way.

Example

The following example checks the AllowEdit property before modifying a row:

DataView view = new DataView(ds.Tables["Customers"]);

// Select the first row.

DataRowView row = view[0];
// Modify it if allowed.
if (view.AllowEdit)
{
    row["CustomerID"] = "NEWCUST";
}
AllowNew

Boolean allowNew = DataView.AllowNew;
DataView.AllowNew = allowNew;

Specifies whether it is possible to add new rows to the underlying DataTable using this DataView. It is the responsibility of the control or application to heed this setting; .NET doesn't enforce it in any way.

Example

The following example checks the AllowNew property before inserting a row:

DataView view = new DataView(ds.Tables["Customers"]);

// Add a row if allowed.
if (view.AllowNew)
{

    DataRowView row = view.AddNew();

    // (Configure columns here.)

    // Commit the new row.
    row.EndEdit();
}
ApplyDefaultSort

Boolean applyDefaultSort = DataView.ApplyDefaultSort;
DataView.ApplyDefaultSort = applyDefaultSort;

If true, the DataView sets a sort order to ascend based on the primary key column of the underlying DataTable. If set to false, this property has no effect. The ApplyDefaultSort property is ignored if the DataView.Sort property isn't a null reference or an empty string. ApplyDefaultSort also has no effect if the underlying DataTable doesn't have a primary key defined.

Example

This example configures a primary key column for a table and uses the column for DataView sorting:

DataTable dt = ds.Tables["Customers"];

// Create a UniqueConstraint object that represents the primary key.
UniqueConstraint uc = new UniqueConstraint("ID", dt.Columns["CustomerID"],
  true);

// Add the UniqueConstraint to the table's Constraints collection.
dt.Constraints.Add(uc);

view = new DataView(dt);

// Use the CustomerID column for sorting.
view.ApplyDefaultSort = true;
Count

Int32 Count = DataView.Count;

Returns the number of rows in the DataView. This may not be the same as the number of rows in the underlying DataTable, depending on the DataView.RowFilter or DataView.RowStateFilter properties.

Example

Here's an example that sets filter selection criteria, and checks how many rows meet the condition:

DataView view = new DataView(ds.Tables["Customers"]);

Console.WriteLine("There are " + view.Count.ToString() + " rows.");

// Find all the rows where a Country isn't specified.
view.RowFilter = "Country IS NULL";

Console.WriteLine("There are " + view.Count.ToString() + 
  " rows that have no specified Country.");
DataViewManager

DataViewManager dvm = DataView.DataViewManager;

Returns the DataViewManager that created this DataTable. If this is the DataTable.DefaultView, this will be the DataSet.DefaultViewManager. If you created the DataView programmatically, the DataViewManager property returns a null reference.

Example

The following code example finds the DataViewManager that created a DataView and then uses that DataViewManager to create a new DataView:

// Retrieve the DataViewManager.
DataViewManager viewManager = view.DataViewManager;

// Use the DataViewManager to create a new DataView.
DataView newView = viewManager.CreateDataView(ds.Tables["Customers"]);
Item

DataRowView row = DataView[Int32 rowIndex];

Retrieves a DataRowView object using the zero-based row index. The Item property is the default indexer for the DataView class.

Example

The following code retrieves the first row and then modifies a field in that DataRow using the DataRowView:

// Retrieve the first row from the view.
DataRowView row = view[0];

// Modify a field in the first row through the DataRowView.
// Because the DataRowView is a window onto the DataView,
// both objects will reflect the changed value.
row["CustomerID"] = "NEWCUST";

Notes

The DataView class also implements the IEnumerable interface, which means you can iterate over its collection of DataRowView objects using the foreach syntax:

DataView view = new DataView(ds.Tables["Customers"]);

// (Configure DataView sorting and filtering here.)

foreach (DataRowView row in view)
{
    // (Do something with the row here.)
}

Remember, you will see only rows from the underlying DataTable that meet the DataView filter conditions.

RowFilter

String filter = DataView.RowFilter;
DataView.RowFilter = filter;

Gets or sets a filter expression that limits the rows that are included in the DataView. The filter expression resembles the WHERE clause of a SQL SELECT statement, so a filter expression of Country = 'Germany' selects only those records in which the Country field contains the string "Germany."

However, you aren't limited to simply equality testing: ADO.NET supports a rich subset of the SQL language for filter expressions, complete with support for aggregate functions, relationships, mathematical operations, and string handling. For a full list of supported operators and several examples, refer to Chapter 12.

Example

The following code statement filters a DataView to include only those rows in which the UnitPrice field is greater than 10:

view.RowFilter = "UnitPrice > 10";

Note

Every time you change the RowFilter property, the contents of the DataView are dynamically updated. Similarly, if you change column values, a row may appear or disappear from the DataView, depending on the filter condition.

RowStateFilter

DataViewRowState state = DataView.RowStateFilter;
DataView.RowStateFilter = state;

Gets or sets a value that determines how rows are filtered based on their DataRowState. You set the RowStateFilter property using one of the values from the DataViewRowState enumeration (or a bitwise combination of values). These values are shown in Table 28-1. By default, the RowStateFilter is set to CurrentRows and shows everything except rows that are scheduled for deletion.

Table 28-1. DataViewRowState values

Value

Description

Added

A new row that is inserted into the data source when the next update is performed.

CurrentRows 

Current rows, including unchanged, new, and modified rows. This is the default.

Deleted

A deleted row that is removed from the data source when the next update is performed.

ModifiedCurrent

A row that exists in the DataSet but has been modified.

ModifiedOriginal

The original version (although it has since been modified and is available as ModifiedCurrent).

None

No rows will be shown.

OriginalRows

Original rows including unchanged and deleted rows.

Unchanged

A row that exists in the DataSet and has not been modified.

Example

The following code snippet configures a view to display only deleted and added rows:

// Show deleted and added rows.
view.RowStateFilter =
    DataViewRowState.Deleted | DataViewRowState.Added;

Note

The values in the DataViewRowState enumeration don't exactly correspond to the values in the DataRowState enumeration. This is because the DataViewRowState must take into account the state of rows (deleted, added, and so on) and indicate which version of the information should be used for display purposes (the current information or the original data queried from the data source).

Sort

String sort = DataView.Sort;
DataView.Sort = sort;

Gets or sets a sort expression that determines how rows are ordered in a DataView. The sort expression resembles the ORDER BY clause of a SQL SELECT statement, so a filter expression of Country ASC orders records alphabetically by country name. If you sort with a numeric data type, numeric sorting is used. If you sort with a character-based data type, alphanumeric sorting applies instead.

ADO.NET supports a rich subset of the SQL language for sort expressions. For a full listing of supported operators and several examples, refer to Chapter 12.

Example

The following code statement orders a DataView by Country and then (if two records have the same Country) by City:

view.Sort = "Country ASC, City ASC";

Notes

Every time you change the Sort property, the contents of the DataView is refreshed. Similarly, if you change a column value that affects sorting, rows may move to new positions in the DataView.

To use the primary key for a sort without defining a Sort expression, set the ApplyDefaultSort property to true.

Table

DataTable dt = DataView.Table;
DataView.Table = dt;

Provides a reference to the underlying DataTable that this DataView exposes. You can read this property to access the full DataTable, or you can modify this property to "point" a DataView to a different DataTable.

Example

The following code retrieves the underlying DataTable and displays some basic information about it in a console window:

DataTable dt = view.Table;

// Print the name and number of rows of the child table.
Console.WriteLine(dt.TableName, dt.Rows.Count.ToString());
    [ Team LiB ] Previous Section Next Section