20080501

GirdView Alphabetic Paging - ASP.net / VB.net

1. Set up the page and controls...
Drag / drop a gridview, a repeater and two labels onto you aspx page.
2. Configure the GridView...
Now we need to set up the GridView for data population and sorting. First, go to your gridview properties and set AllowPaging and AllowSorting to "True".
Then, select the gridview on your page layout and click the properties triangle in the right-hand corner and select "Edit Columns...".
Since this GridView is going to be unbound, we need to add the columns manually. In this example I will be pulling in three fields - "FirstName", "LastName" and "City". So select the "BoundField" option in the upperleft-hand corner of the Fields dialog, and click the "Add" button. On the right-hand side of the dialog under "BoundField properties", enter the SQL field name under in the DataField and SortExpression properties. You can name the column whatever you want in the HeaderText property. Repeat this process for all fields in your GridView.
3. Final page layout...
Now, set the text in Label1 = "Filter" and the text in Label2 = "All". Your page layout should look something like this...
4. Alphabetical paging / sorting...
First, we need to edit the source on the .aspx page. Switch to source view and update the properties for your Repeater so it matches the example below...
Setting the Text and CommandArgument properties to these variables will allow us to dynamically populate our Repeater control with link buttons that will fire the Repater1_ItemCommand event and pass the CommandName "AlphabeticalPager" to the vb code. Swtich back to design view and you should see your Repeater looks a little different...
5. Let's add some code!
Go to your web.config and set up your appsetting connection...

Then, write a function to populate the GridView and set that function to run at Page_Load...
Now, we need to create some custom GridView sorting expressions and properties so we can sort on any field in the GridView...
Now, set up a function to populate the Repeater with the alphabet and set up the code to execute when an item in the Repeater is chosen by the user. There is also an extra GridView page change event that we need to add...
That's it! Just set the Show_All_Records() and BuildAlphabet() to run on the Page_Load.