Tài liệu Finding Rows in a DataView doc - Pdf 87

[ Team LiB ]Recipe 3.9 Finding Rows in a DataView
Problem
You need to find a row or group of rows in a DataView meeting certain criteria.
Solution
Use a sorted DataView to find rows using columns that are not part of the primary key.
The sample code contains two event handlers:
Form.Load
Sets up the sample by creating a DataTable containing the Orders table from the
Northwind database. A DataView of the table sorted by the CustomerID and
EmployeeID is created.
Find Button.Click
Uses the FindRows( ) method of the DataView to retrieve the array of
DataRowView objects matching the CustomerID and OrderID specified. The code
iterates over the collection to return the results.
The C# code is shown in Example 3-9
.
Example 3-9. File: DataViewSearchPerformanceForm.cs
// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Text;
using System.Data;
using System.Data.SqlClient;

private DataView dv;

// . . .


}

// Display the results.
if(foundRows.Length == 0)
{
result.Append("No rows found.");
}
else
{
result.Append("ORDER\tREQUIRED DATE" + Environment.NewLine);

// Iterate over the collection of found rows.
foreach(DataRowView row in foundRows)
{
result.Append(row["OrderID"] + "\t" +
row["RequiredDate"] +
Environment.NewLine);
}

result.Append("COUNT\t" + foundRows.Length +
Environment.NewLine);
}

resultTextBox.Text = result.ToString( );
}
Discussion
The Find( ) and FindRows( ) methods of the DataView search for rows in a DataView
using its sort key values. The search values must match the sort key values exactly to
return a result; wild card matches are not possible.
The primary difference between the Find( ) and FindRows( ) methods is that Find( )


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status