Chapter 7. Sorting the DataGrid s Data

Chapter 7. Sorting the DataGrid's Data

IN THIS CHAPTER

  • Providing Simple Sorting Support in the DataGrid Web Control

    • Adding Advanced Sorting Features

    • Caching the DataGrid's DataSource at the Page-Level

    • On the Web

In previous code examples, we have demonstrated listing the contents of the pubs database's titles table. Recall that the titles table contains information about various books, such as their title, the price, the publication date, and so on. Now, imagine that we want to display the contents of the titles table in a data Web control with the results ordered alphabetically by the book's title.

This task may seem quite trivial: All we would need to do is alter our SQL query, adding an ORDER BY clause that specifies a proper ordering. For example, the following SQL query would suffice:

 SELECT title, price, royalty, pubdate  FROM titles  ORDER BY title 

By populating a DataSet or DataReader with this SQL query, then setting the data Web control's DataSource property to the resulting DataSet or DataReader, and finally calling the data Web control's DataBind() method, we would have a listing of the books in the titles table ordered alphabetically.

This approach, unfortunately, doesn't give much flexibility to the user visiting the Web page. What if he wants to view the contents of the titles table ordered by the price of the book, from the most expensive to the least expensive? Or maybe he wants to view the books from the most recently published book to the least recently published one. To provide such functionality, we could create a set of external sorting command buttons, much like we did in the last chapter to create the external filtering buttons.

Fortunately, the DataGrid Web control provides built-in sorting functionality. As we will see in this chapter, the DataGrid's sorting functionality adds a HyperLink command button to each column's header. When this command button is clicked, the DataGrid's SortCommand event is raised. By providing an event handler for this event, we can determine when the user opts to sort the DataGrid's data. At that point, we can re-query the database, retrieve the data in the desired sort order, and then rebind the data to the DataGrid.

NOTE

Alternatively, as we saw in Chapter 6, "Using External Web Controls to Achieve Effects on a Data Web Control," we can use ViewState caching techniques to cache the DataGrid's data and then sort that cached version, which ends up saving a database call. We will examine both approaches again in this chapter.




ASP. NET Data Web Controls Kick Start
ASP.NET Data Web Controls Kick Start
ISBN: 0672325012
EAN: 2147483647
Year: 2002
Pages: 111

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net