Export (0) Print
Expand All

DataTable.Select Method (String, String, DataViewRowState)

Gets an array of all DataRow objects that match the filter in the order of the sort that match the specified state.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

public DataRow[] Select (
	string filterExpression,
	string sort,
	DataViewRowState recordStates
)
public DataRow[] Select (
	String filterExpression, 
	String sort, 
	DataViewRowState recordStates
)
public function Select (
	filterExpression : String, 
	sort : String, 
	recordStates : DataViewRowState
) : DataRow[]

Parameters

filterExpression

The criteria to use to filter the rows.

sort

A string specifying the column and sort direction.

recordStates

One of the DataViewRowState values.

Return Value

An array of DataRow objects.

To form the filterExpression argument, use the same rules for creating the DataColumn class's Expression property value. The Sort argument also uses the same rules for creating class's Expression strings.

The following example uses a filter expression and record state to return an array of DataRow objects.

private static void GetRowsByFilter()
{
    DataTable customerTable = new DataTable("Customers");
    // Add columns
    customerTable.Columns.Add("id", typeof(int));
    customerTable.Columns.Add("name", typeof(string));

    // Set PrimaryKey
    customerTable.Columns[ "id" ].Unique = true;
    customerTable.PrimaryKey = new DataColumn[] 
        { customerTable.Columns["id"] ;

    // Add ten rows
    for(int id=1; id<=10; id++)
    {
        customerTable.Rows.Add(
            new object[] { id, string.Format("customer{0", id) );
    
    customerTable.AcceptChanges();

    // Add another ten rows
    for(int id=11; id<=20; id++)
    {
        customerTable.Rows.Add(
            new object[] { id, string.Format("customer{0", id) );
    

    string expression;
    string sortOrder;

    expression = "id > 5";
    // Sort descending by column named CompanyName.
    sortOrder = "name DESC";
    // Use the Select method to find all rows matching the filter.
    DataRow[] foundRows = 
        customerTable.Select(expression, sortOrder, 
        DataViewRowState.Added);

    PrintRows(foundRows, "filtered rows");

    foundRows = customerTable.Select();
    PrintRows(foundRows, "all rows");


private static void PrintRows(DataRow[] rows, string label)
{
    Console.WriteLine("\n{0", label);
    if(rows.Length <= 0)
    {
        Console.WriteLine("no rows found");
        return;
    
    foreach(DataRow row in rows)
    {
        foreach(DataColumn column in row.Table.Columns)
        {
            Console.Write("\table {0", row[column]);
        
        Console.WriteLine();
    


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft