The Table controls and DataRepeater are fine when I have small sets of data, but the display just keeps repeating and I have to write a bunch of code to change the sort order of the data. How do I create a table-like display that will show a set number of rows at a time and let me sort the data?
The DataGrid control is by far the most flexible and powerful of the controls used in ASP.NET for displaying data on your Web page. It gives you the ability not only to list data, but also to page through it and sort it nicely with headers, and even to edit data. This last feature will be saved for the next How-To.
To perform the other tasks, you will use a combination of setting properties at design time and programming some events. In particular for the DataGrid control, you will add code to events raised by sorting and paging.
This How-To will use the DataGrid bound at design time. You will also see a simple example of using the DataView object for sorting your data.
Open and run the Visual Basic .NET -Chapter 5 solution. From the main page, click on the hyperlink with the caption How-To 5.6: Display, Sort, and Page Data in the DataGrid Control. You will then see all the territories loaded into a data grid. You can click the column headings, displayed in blue, to sort the columns, and you can click the arrows at the bottom to page through the data. You can see the Web Form in Design mode in Figure 5.8.
Create a Web Form. Then place the controls in Table 5.9 and Figure 5.8 with the following properties set.
Object |
Property |
Setting |
---|---|---|
OleDbDataAdapter |
ID |
odaTerritory |
SelectCommand |
SELECT Territories.TerritoryID, Territories.TerritoryDescription, Region.RegionDescription, Region.RegionID FROM Territories INNER JOIN Region ON Territories.RegionID = Region.RegionID |
|
DataSet |
ID |
dsTerritory |
DataView |
ID |
dvTerritory |
Table |
dsTerritory.Territories |
|
DataGrid |
ID |
dgTerritory |
HyperLink |
ID |
hplReturnToMain |
NavigateURL |
wfrmMain.aspx |
Right-click on the DataGrid control and choose Property Builder. You will then see the General tab of the DataGrid Property Builder. Set the properties as displayed in Figure 5.9.
Click on the Columns tab. You can now add the columns TerritoryID, TerritoryDescription, and RegionDescription by clicking on the field in the available columns and then clicking the Add button for each one. You can then set the Header text for each to be TerritoryID, Territory, and Region respectively, by clicking on the column in the Selected Column list and changing the Header text property. When you are finished and you have the Region highlighted, it should look like Figure 5.10.
Note
Be sure to uncheck the Create Columns Automatically at Run Time check box; otherwise, the columns will end up showing up twice-once from you specifying them here at design time, and once when the code generates them. |
Click on the Paging tab. Click on the Allow Paging check box, and type 10 for the Page Size (see Figure 5.11.) You can then close the Property Builder dialog box by clicking OK.
Create the BindTheGrid routine in Listing 5.19. Note that you are now binding to the DataView, rather than to a dataset.
Sub BindTheGrid() odaTerritory.Fill(dsTerritory) dgTerritory.DataSource = dvTerritory dgTerritory.DataBind() End Sub
Add the code in Listing 5.20 to the Load event of the page.
Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not Me.IsPostBack Then BindTheGrid() End If End Sub
Add the code in Listing 5.21 to the PageIndexChanged event of dgTerritory. This routine takes the NewPageIndex and assigns it to the data grid's CurrentPageIndex. Next, if MySort exists in the ViewState object, it is assigned to the DataView control. MySort is stored in the next step.
Private Sub dgTerritory_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgTerritory.PageIndexChanged '-- Set the current page in the data grid Me.dgTerritory.CurrentPageIndex = e.NewPageIndex If Not (ViewState("MySort") = Nothing) Then dvTerritory.Sort = ViewState("MySort") End If BindTheGrid() End Sub
Add the code in Listing 5.22 to the SortCommand event of dgTerritory. When a column heading is clicked and sorting is turned on, this event is raised. This routine assigns the SortExpression to the Sort property of the dgTerritory. The SortExpression property is then stored in the ViewState object for round trips to the server, and then it is used in the code listed in step 6.
Private Sub dgTerritory_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgTerritory.SortCommand dvTerritory.Sort = e.SortExpression.ToString ViewState("MySort") = e.SortExpression.ToString BindTheGrid() End Sub
The DataView is a great control for seeing different views of your data by using a single dataset.
As with the DataRepeater, working with some of the basic features in the data grid is pretty easy. It's when you have to start really manipulating data that it gets more difficult, as you will see in the next How-To.