DataTable.Select Method
Gets an array of DataRow objects.
Overload List
Gets an array of all DataRow objects.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Select() As DataRow()
[C#] public DataRow[] Select();
[C++] public: DataRow* Select() [];
[JScript] public function Select() : DataRow[];
Gets an array of all DataRow objects that match the filter criteria in order of primary key (or lacking one, order of addition.)
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Select(String) As DataRow()
[C#] public DataRow[] Select(string);
[C++] public: DataRow* Select(String*) [];
[JScript] public function Select(String) : DataRow[];
Gets an array of all DataRow objects that match the filter criteria, in the specified sort order.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Select(String, String) As DataRow()
[C#] public DataRow[] Select(string, string);
[C++] public: DataRow* Select(String*, String*) [];
[JScript] public function Select(String, String) : DataRow[];
Gets an array of all DataRow objects that match the filter in the order of the sort, that match the specified state.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function Select(String, String, DataViewRowState) As DataRow()
[C#] public DataRow[] Select(string, string, DataViewRowState);
[C++] public: DataRow* Select(String*, String*, DataViewRowState) [];
[JScript] public function Select(String, String, DataViewRowState) : DataRow[];
Example
[Visual Basic, C#, C++] The following example uses a filter expression and record state to return an array of DataRow objects.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Select. For other examples that might be available, see the individual overload topics.
[Visual Basic] Private Sub GetRowsByFilter() Dim customerTable As DataTable customerTable = new DataTable( "Customers" ) ' Add columns customerTable.Columns.Add( "id", GetType(Integer) ) customerTable.Columns.Add( "name", GetType(String) ) ' Set PrimaryKey customerTable.Columns("id").Unique = true customerTable.PrimaryKey = new DataColumn() { customerTable.Columns("id") } ' add ten rows Dim id As Integer For id = 1 To 10 customerTable.Rows.Add( _ new object() { id, string.Format("customer{0}", id) } ) Next id customerTable.AcceptChanges() ' add another ten rows For id = 11 To 20 customerTable.Rows.Add( _ new object() { id, string.Format("customer{0}", id) } ) Next id Dim strExpr As String Dim strSort As String strExpr = "id > 5" ' Sort descending by CompanyName column. strSort = "name DESC" ' Use the Select method to find all rows matching the filter. Dim foundRows As DataRow() = _ customerTable.Select( strExpr, strSort, DataViewRowState.Added ) PrintRows( foundRows, "filtered rows") foundRows = customerTable.Select() PrintRows( foundRows, "all rows") End Sub Private Sub PrintRows( rows() As DataRow, label As String) Console.WriteLine( "\n{0}", label ) If rows.Length <= 0 Then Console.WriteLine( "no rows found" ) Exit Sub End If Dim r As DataRow Dim c As DataColumn For Each r In rows For Each c In r.Table.Columns Console.Write( "\t {0}", r(c) ) Next c Console.WriteLine() Next r End Sub [C#] 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 strExpr; string strSort; strExpr = "id > 5"; // Sort descending by column named CompanyName. strSort = "name DESC"; // Use the Select method to find all rows matching the filter. DataRow[] foundRows = customerTable.Select( strExpr, strSort, 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 r in rows ) { foreach( DataColumn c in r.Table.Columns ) { Console.Write( "\t {0}", r[c] ); } Console.WriteLine(); } } [C++] private: static void GetRowsByFilter() { DataTable* customerTable = new DataTable( S"Customers" ); // Add columns customerTable->Columns->Add( S"id", __typeof(int) ); customerTable->Columns->Add( S"name", __typeof(String) ); // Set PrimaryKey customerTable->Columns->Item[ S"id" ]->Unique = true; DataColumn* temp2 [] = {customerTable->Columns->Item[S"id"]}; customerTable->PrimaryKey = temp2; // Add ten rows for( int id=1; id<=10; id++ ) { Object* temp0 [] = {__box(id), String::Format(S"customer{0}", __box(id))}; customerTable->Rows->Add( temp0 ); } customerTable->AcceptChanges(); // Add another ten rows for( int id=11; id<=20; id++ ) { Object* temp1 [] = {__box(id), String::Format(S"customer{0}", __box(id))}; customerTable->Rows->Add( temp1 ); } String* strExpr; String* strSort; strExpr = S"id > 5"; // Sort descending by column named CompanyName. strSort = S"name DESC"; // Use the Select method to find all rows matching the filter. DataRow* foundRows[] = customerTable->Select( strExpr, strSort, DataViewRowState::Added ); PrintRows( foundRows, S"filtered rows" ); foundRows = customerTable->Select(); PrintRows( foundRows, S"all rows" ); } static void PrintRows( DataRow* rows[], String* label ) { Console::WriteLine( S"\n{0}", label ); if( rows->Length <= 0 ) { Console::WriteLine( S"no rows found" ); return; } System::Collections::IEnumerator* myEnum = rows->GetEnumerator(); while (myEnum->MoveNext()) { DataRow* r = __try_cast<DataRow*>(myEnum->Current); System::Collections::IEnumerator* myEnum1 = r->Table->Columns->GetEnumerator(); while (myEnum1->MoveNext()) { DataColumn* c = __try_cast<DataColumn*>(myEnum1->Current); Console::Write( S"\t {0}", r->Item[c] ); } Console::WriteLine(); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.