15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

st<strong>and</strong>ard Controls <strong>and</strong> Components ❘ 1133<br />

figure 39-6<br />

The filter not only applies to the visible rows but also to the state of the columns within those rows. This is<br />

evident when choosing the ModifiedOriginal or ModifiedCurrent selections. These states are based on<br />

the DataRowVersion enumeration. For example, when the user has updated a column in the row, the<br />

row will be displayed when either ModifiedOriginal or ModifiedCurrent is chosen; however, the actual<br />

value will be either the Original value selected from the database (if ModifiedOriginal is chosen) or the<br />

current value in the DataColumn (if ModifiedCurrent is chosen).<br />

Sorting Rows<br />

Apart from filtering data, you might also have to sort the data<br />

within a DataView. To sort data in ascending or descending order,<br />

simply click the column header in the DataGridView control (see<br />

Figure 39-7). The only trouble is that the control can sort by only<br />

one column, whereas the underlying DataView control can sort by<br />

multiple columns.<br />

When a column is sorted, either by clicking the header (as shown on the SupplierId column) or in code,<br />

the DataGrid displays an arrow bitmap to indicate which column the sort has been applied to.<br />

To set the sort order on a column programmatically, use the Sort property of the DataView:<br />

dataView.Sort = "ProductName";<br />

dataView.Sort = "ProductName ASC, ProductID DESC";<br />

figure 39-7<br />

The first line sorts the data based on the ProductName column, as shown in Figure 39-7. The second line<br />

sorts the data in ascending order, based on the ProductName column, then in descending order of ProductID.<br />

The DataView supports both ascending (default) <strong>and</strong> descending sort orders on columns. If more than one<br />

column is sorted in code in the DataView, the DataGridView will cease to display any sort arrows.<br />

Each column in the grid can be strongly typed, so its sort order is not based on the string representation of<br />

the column but instead is based on the data within that column. The upshot is that if there is a date column<br />

in the DataGrid, the user can sort numerically on the date rather than on the date string representation.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!