Viewing Data in a DataTable

You can access the contents of a DataTable by using the Rows and Columns collections of the DataTable. You can also use the Select method to return subsets of the data in a DataTable according to 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.


If you are 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 indexes the rows of the table. The Select method then uses that index, significantly reducing the time to generate the query result. For information about creating a DataView for a DataTable, see 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.

DataViewRowState value 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 column As DataColumn
Dim row As DataRow

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

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

  Console.WriteLine(vbTab & "RowState")

  For Each row In currentRows
    For Each column In workTable.Columns
      Console.Write(vbTab & row(column).ToString())

    Dim rowState As String = _
        System.Enum.GetName(row.RowState.GetType(), row.RowState)
    Console.WriteLine(vbTab & rowState)
End If
DataRow[] currentRows = workTable.Select(
    null, null, DataViewRowState.CurrentRows);

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


  foreach (DataRow row in currentRows)
    foreach (DataColumn column in workTable.Columns)
      Console.Write("\t{0}", row[column]);

    Console.WriteLine("\t" + row.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 deletedRows() 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")
// Retrieve all deleted rows.
DataRow[] deletedRows = 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