DekGenius.com
[ Team LiB ] Previous Section Next Section

28.3 Methods Reference

AddNew

DataRowView = DataView.AddNew();

Inserts a new row into the underlying DataTable and returns a DataRowView object that represents the new row. The DataView.AddNew( ) method immediately adds the new row to the DataTable and implicitly calls BeginEdit( ) on the row. You must set all required values (values for columns that don't allow nulls and don't have default values) and then call DataRowView.EndEdit( ) to commit changes.

Example

Here's an example that creates a new row for the Categories table using the DataView.AddNew( ) method:

DataRowView row = view.AddNew();

// Set all required values.
row["CategoryName"] = "Fruit";
row["Description"] = "Pears, kiwis, and oranges";

// Commit the new row.
view.EndEdit();

Note

It's possible that the new DataRowView won't be visible in the DataView you used to create it, depending on the value of the DataView.RowFilter and DataView.RowStateFilter properties.

Delete

DataView.Delete(Int32 rowIndex);

Removes a row at the specified index. This affects both the DataView and the underlying DataTable. As with the DataRow.Delete( ) method, the DataView.Delete( ) method marks a row for deletion by setting the DataRow.RowState property. The change doesn't become final until you call the DataTable.AcceptChanges( ) method (either directly or implicitly through the DataAdapter.Update( ) method).

Example

This code snippet deletes all order records in which the UnitPrice field is equal to 0.

DataView view = new DataView(ds.Tables["Orders"]);
view.RowFilter = "UnitPrice = 0";

// Delete these rows.
foreach (DataRowView row in view)
{
    row.Delete();
}
Find

Int32 rowIndex = DataView.Find(Object sortKey);
Int32 rowIndex = DataView.Find(Object[] sortKey);

Returns the index of a single matching row, using the current DataView sort order. For example, if you have a sort defined on the ContactName column of the Customers table, you can use the Find( ) method to search for a row with a specific ContactName. If no match is found, the Find( ) method returns -1. If there are multiple matches, only the first is returned.

The Find( ) method requires exact matches. You can't perform a partial match (for example, by supplying just a first name for the ContactName) or use a wildcard.

Parameters

Object sortKey
Object[] sortKey

The sortKey specifies the value you are searching for. The data type you use should match the data type of the column used for the search criteria. If you create a sort expression that incorporates information from multiple columns, you must use the overloaded version of the Find( ) method that accepts an array with search values for all columns, in the same order.

Example

The following code snippet searches for an exact match of the ContactName field:

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

int rowIndex = view.Find("Roland Mendel");

if (rowIndex == -1)
{
    Console.WriteLine("No match found.");
}
else
{
    Console.WriteLine(view[rowIndex]["CustomerID"].ToString() + 
                      " is a match.");

}

Notes

To use the Find( ) method, you must have defined a sort order for a DataView, either by setting the RowFilter property or the ApplyDefaultSort property. If it's possible that your search will match more than one row, use the FindRows( ) method instead of the Find( ) method.

The case-sensitivity of search values for the Find( ) method is determined by the CaseSensitive property of the underlying DataTable.

FindRows

DataRowView[] rows = DataView.FindRows(Object sortKey);
DataRowView[] rows = DataView.FindRows(Object[] sortKey);

Returns an array with every DataRowView object that matches a specified search expression in a given DataView. If no match is found, FindRows( ) returns an empty array.

The FindRows( ) method requires exact matches. You can't perform a partial match (for example, by supplying just a first name for the ContactName).

Parameters

Object sortKey
Object[] sortKey

The sortKey specifies that value you are searching for. The data type you use must match the data type of the column used for the search criteria. If you create a sort expression that incorporates information from multiple columns, you must use the overloaded version of the FindRows( ) method that accepts an array with search values for all columns. These search values must use the same order as the columns in the sort expression.

Example

The following code snippet searches for an exact match of the Country field and displays the information for all matching rows:

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

DataRowView[] rows = view.FindRows("Germany");
if (rows.Length == 0)
{
    Console.WriteLine("No match found.");
}
else
{
    foreach (DataRowView row in rows)
    {
        Console.WriteLine(row["CustomerID"].ToString() + " is a match.");
    }
}

Notes

To use the FindRows( ) method, you must define a sort order for a DataView, either by setting the RowFilter property or the ApplyDefaultSort property. If you want to search a unique column, you can use the Find( ) method instead of the FindRows( ) method for slightly easier coding.

The case-sensitivity of search values for the FindRows( ) method is determined by the CaseSensitive property of the underlying DataTable.

    [ Team LiB ] Previous Section Next Section