Viewing Data in a Table

You can access the contents of a DataTable using the Rows and Columns collections of the DataTable. You can also use the DataTable.Select method to return subsets of the data in a DataTable according to certain criteria including search criteria, sort order, and row state. Additionally, you can use the Find method of the DataRowCollection when searching for a particular row using a primary key value.

The Select method of the DataTable object returns a set of DataRow objects that match the specified criteria. Select takes optional arguments of a filter expression, sort expression, and DataViewRowState. The filter expression identifies which rows to return based on DataColumn values, such as LastName = 'Smith'. The sort expression follows standard SQL conventions for ordering columns, for example LastName ASC, FirstName ASC. For rules about writing expressions, see the Expression property of the DataColumn class.

**Tip   **If you will be performing a number of calls to the Select method of a DataTable, you can increase performance by first creating a DataView for the DataTable. Creating the DataView will index the rows of the table. The Select method will then use that index, significantly reducing the time to generate the query result. For information about creating a DataView for a DataTable, see Creating and Using DataViews.

The Select method determines which version of the rows to view or manipulate based on a DataViewRowState. The following table describes the possible DataViewRowState enumeration values.

Member name Description
CurrentRows Current rows including unchanged, added, and modified rows.
Deleted A deleted row.
ModifiedCurrent A current version, which is a modified version of original data. (See ModifiedOriginal.)
ModifiedOriginal The original version of all modified rows. The current version is available using ModifiedCurrent.
Added A new row.
None None.
OriginalRows Original rows, including unchanged and deleted rows.
Unchanged An unchanged row.

In the following example, the DataSet object is filtered so that you are only working with rows whose DataViewRowState is set to CurrentRows.

Dim myCol As DataColumn
Dim myRow As DataRow

Dim currRows() As DataRow = workTable.Select(Nothing, Nothing, DataViewRowState.CurrentRows)

If (currRows.Length < 1 ) Then
  Console.WriteLine("No Current Rows Found")
Else
  For Each myCol in workTable.Columns
    Console.Write(vbTab & myCol.ColumnName)
  Next

  Console.WriteLine(vbTab & "RowState")

  For Each myRow In currRows
    For Each myCol In workTable.Columns
      Console.Write(vbTab & myRow(myCol).ToString())
    Next

    Dim rowState As String = System.Enum.GetName(myRow.RowState.GetType(), myRow.RowState)
    Console.WriteLine(vbTab & rowState)
  Next
End If
[C#]
DataRow[] currRows = workTable.Select(null, null, DataViewRowState.CurrentRows);

if (currRows.Length < 1 )
  Console.WriteLine("No Current Rows Found");
else
{
  foreach (DataColumn myCol in workTable.Columns)
    Console.Write("\t{0}", myCol.ColumnName);

  Console.WriteLine("\tRowState");

  foreach (DataRow myRow in currRows)
  {
    foreach (DataColumn myCol in workTable.Columns)
      Console.Write("\t{0}", myRow[myCol]);

    Console.WriteLine("\t" + myRow.RowState);
  }
}

The Select method can be used to return rows with differing RowState values or field values. The following example returns a DataRow array that references all rows that have been deleted, and returns another DataRow array that references all rows, ordered by CustLName, where the CustID column is greater than 5. For information about how to view the information in the Deleted row, see Row States and Row Versions.

' Retrieve all deleted rows.
Dim delRows() As DataRow = workTable.Select(Nothing, Nothing, DataViewRowState.Deleted)

' Retrieve rows where CustID > 5, and order by CustLName.
Dim custRows() As DataRow = workTable.Select("CustID > 5", "CustLName ASC")
[C#]
// Retrieve all deleted rows.
DataRow[] delRows = workTable.Select(null, null, DataViewRowState.Deleted);

// Retrieve rows where CustID > 5, and order by CustLName.
DataRow[] custRows = workTable.Select("CustID > 5", "CustLName ASC");

See Also

Manipulating Data in a DataTable | Row States and Row Versions | DataRow Class | DataSet Class | DataTable Class | DataViewRowState Enumeration