The datatable. Select method returns an array of all datarow objects. Select () has four overload methods.
|
Name |
Description |
|
Select () |
Obtains the array of all datarow objects. |
|
Select (string) |
Obtain the array of all datarow objects that match the filtering conditions according to the primary key order (if no primary key exists, the addition order is followed. |
|
Select (string, string) |
Obtains an array of all datarow objects in the specified sorting order that matches the filtering conditions. |
|
Select (string, String, dataviewrowstate) |
Obtains the array of all datarow objects that match the filters in the sorting order and the specified status. |
1. Select () Example:
Private
Void getrows ()
{
// Get the datatable of a dataset.
Datatable table = dataset1.tables ["Suppliers"];
Datarow [] rows = table. Select ();
// Print the value one column of each datarow.
For (INT I = 0; I <rows. length; I ++)
{
Console. writeline (rows [I] ["companyName"]);
}
}
2. Select (string
Filterexpression) Example:
Private
Void getrowsbyfilter ()
{
Datatable table = dataset1.tables ["orders"];
// Presuming the datatable has a column named date.
String expression;
Expression = "date >#1/1/00 #";
Datarow [] foundrows;
// Use the select method to find all rows matching the filter.
Foundrows = table. Select (expression );
// Print column 0 of each returned row.
For (INT I = 0; I <foundrows. length; I ++)
{
Console. writeline (foundrows [I] [0]);
}
}
3. Select (String filterexpression, string sort) Example:
Private
Void getrowsbyfilter ()
{
Datatable table = dataset1.tables ["orders"];
// Presuming the datatable has a column named date.
String expression =
"Date> '2014/1/00 '";
// Sort Descending by column named companyName.
String sortorder =
"CompanyName DESC ";
Datarow [] foundrows;
// Use the select method to find all rows matching the filter.
Foundrows = table. Select (expression, sortorder );
// Print column 0 of each returned row.
For (INT I = 0; I <foundrows. length; I ++)
{
Console. writeline (foundrows [I] [0]);
}
}
4. Select (String filterexpression, string sort, dataviewrowstate
Recordstates) Example:
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 ();
}
}