Select method of DataTable
Obtains an array of DataRow objects.
Reload list
Description
Select () gets 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.
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"]);
}
}
//************************************** **************************************** ****************************
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]); }}
//************************************** **************************************** ****************************
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]);
}
}
//************************************** **************************************** ****************************
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 ();
}
}