DataTable.Select method (String, String, DataViewRowState)
Gets arrays of all DataRow objects that match the filters in the sort order and the specified state.
[Visual Basic]
Overloads public function select (_
Byval FilteRexpression As String, _
Byval sort as string, _
Byval RecordStates As DataViewRowState_
) As dataroow ()
[C #]
Public DataRow [] SELECT
String FilteRexpression,
String sort,
DataViewRowState RecordStates
);
[C ]
PUBLIC: DATAROW * SELECT
String * FilteRexpression,
String * sort,
DataViewRowState RecordStates
[];
[Jscript]
Public Function SELECT
FilteRexpression: String,
Sort: String,
RecordStates: DataViewRowState
: DATAROW [];
parameter
Filterexpression
To screen the conditions for screening.
sort
A string, which specifies the column and sorting direction.
ReCordStates
One of the DataViewRowState values.
return value
Array of DataRow objects.
Note
To form a FilteRexpression parameter, use the same rules as the EXPRESSION attribute value that creates a DataColumn class. The Sort parameter also uses the same rules as the EXPRESSION string that creates class.
Example
[Visual Basic, C #, C ] The following example uses a filter expression and record status to return to the array of DataRow objects.
[Visual Basic]
Private sub getrowsbyfilter ()
DIM Customertable as DataTable
Customertable = New DataTable ("Customers")
'Add Column
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 Streetpr 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 strs.
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.column)
{
Console.write ("/ T {0}", R [C]);
}
Console.writeLine ();
}
}
[C ]
Private:
Static void getRowsbyfilter ()
{
DataTable * Customertable = New DataTable (s "Customers");
// Add columns
Customertable-> Column-> add (s "id", __typeof (int));
CustomERTABLE-> Column-> Add (s "name", __typeof (string));
// set primarykey
CustomERTABLE-> Column-> Item [S "ID"] -> unique = true;
Datacolumn * Temp2 [] = {Customertable-> Column-> 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 * strs.
Strexpr = s "ID> 5";
// sort descending by column named companyname.strsort = s "name dec";
// 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
System :: Collectes :: IEnumerator * myenum1 = r-> Table-> columns-> getenumerator ();
While (MyEnum1-> MoveNext ())
{
Datacolumn * c = __TRY_CAST
Console :: Write (s "/ t {0}", r-> item [c]);
}
Console :: WriteLine ();
}
}
[JScript] There is no example available for JScript. To view Visual Basic, C # or C examples, click the "Language Filter" button in the upper left corner.
Claim
Platform: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series, .NET Framework Lite
See
DataTable Class | DataTable Member | System.Data Name Space | DataTable.Select Overloaded list | CaseSensitive | DataRow | DataView | DataViewRowState | Expression