Sorting and Filtering
DataView object also gives you the opportunity
to apply sorting and filtering logic that customizes how data will
appear without modifying the underlying data itself.
Sorting with the DataView
To apply a sort
to bound data, you simply set the
property with a string with the corresponding sort information.
ADO.NET sorting uses the same syntax as the ORDER BY clause in a SQL
query. For example, you might use the following SQL statement to
order results by country:
SELECT * FROM Customers ORDER BY Country ASC
The equivalent ADO.NET code is shown here:
ds.Tables["Customers"].DefaultView.Sort = "Country ASC"; dataGrid1.DataSource = ds.Tables["Customers"];
The sort is according to the sort order of the data type of the
column. For example, string columns are sorted alphanumerically
without regard to case (assuming the
DataTable.CaseSensitive property is
false). Numeric columns are ordered using a
numeric sort. Columns that contain binary data can’t
be sorted. Add ASC after a column name for an ascending sort (with
smallest values first) or DESC for a descending sort.
Keep in mind that if you want to bind a control to the full
DataSet, setting the
DataView.Sort property will have no effect because
DataView isn’t used.
Instead, you must modify the
property exposed through the
ds.DefaultViewManager.DataViewSettings["Customers"].Sort = "Country ASC"; dataGrid1.DataSource = ds;
DataGrid binding is dynamic ...