Skip to main content

Wraps the usage of some DataTable.DefaultView properties to return sorted rows filtered on a custom filter expression.

public static DataTable SelectRows(this DataTable dt, string whereExpression, string orderByExpression)
{
    dt.DefaultView.RowFilter = whereExpression;
    dt.DefaultView.Sort = orderByExpression;

    return dt.DefaultView.ToTable();
}

//
// Example
DataTable selection = dt.SelectRows("ID > 10", "Name");