Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: ASP.NET FAQ


How can I add auto-reverse to my datagrid? by link9
Posted: 12 Jun 02 (Edited 15 Jun 02)

Note -- I'm sorry the formatting is bad on this FAQ.  Some lines are just too wide.  If you copy and paste it into your favorite editor, it will look much better

This will be a two-fold FAQ.  First of all, "How do I make my own custom control?", and second, "How can I easily add autoreverse sorting to my datagrid?"

So we'll extend the datagrid through this class and expose one extra property and one extra function to add the feature.

So why didn't Microsoft put autoreverse sorting into the datagrid in the first place?  Who knows.  But furthermore...  who cares when it's this easy to add it yourself!  So without further ado:

Imports System.ComponentModel
Imports System.Web.UI

<ToolboxData("<{0}:reversableGrid runat=server></{0}:reversableGrid>")> Public Class reversableGrid
        Inherits System.Web.UI.WebControls.DataGrid

        Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
        End Sub

        Public ReadOnly Property sortExpression() As String
                Dim output As String
                output = sortField
                If Not sortAscending Then
                    output &= " DESC"
                End If
                Return output
            End Get
        End Property

        Public Property sortField() As String
                Dim o As Object = ViewState.Item("sortField")
                Dim output As String
                If o Is Nothing Then
                    output = String.Empty
                    output = o.ToString()
                End If
                Return output
            End Get
            Set(ByVal Value As String)
                If Value = sortField Then
                    sortAscending = Not sortAscending
                End If
                ViewState.Item("sortField") = Value
            End Set
        End Property

        Private Property sortAscending() As Boolean
                Dim o As Object = ViewState.Item("sortAscending")
                Dim output As String
                If o Is Nothing Then
                    output = True
                    output = CBool(o)
                End If
                Return output
            End Get
            Set(ByVal Value As Boolean)
                ViewState.Item("sortAscending") = Value
            End Set
        End Property

    End Class

NOTE -- ViewState will have to be enabled on the page where you intend to use this in order for it to work

And that's that.  Here's an example of how to use it.

First you'll need to put this into a separate project.  This project needs to be of type "Web Control Library" and it's right there on your "New Project" dialog.  Rename that class to something meaningful like 'reversableGrid.vb' or whatever you want.

Next thing I'm going to suggest is that you kill the root namespace on the project.  When you're building a library, you don't want Microsoft deciding what your namespace hierarchy is going to look like.  You should decide that.  So to do this, just right click on the project, and ask for Properties.  In there, there's a 'root namespace' box that has the name of your project in it.  Just wipe that out.  Once you do that, though, you'll do yourself a favor to compile this class into a new namespace so that you can keep track of it.  But I digress.  This is a topic for another FAQ.

Ok.  Now you've got your project and you've compiled it.  Next, you need to go to a web project and add a reference to this project.  Check.

Then, on the page where you want to use this control, you need to put the following directive:

<%@ Register TagPrefix="Link9" Namespace="Link9.customControls" Assembly="reversableGrid" %>

^^Now I'm assuming a few things here...    But just modify as needed.^^

Then, to use it on the page, just:
<Link9:reversableGrid id="Link9Grid" runat="server" AllowSorting="True" />

And that's that.  Not only can you use it like a regular datagrid (don't ya just love implementation inheritance?!?!), but you even have access to the supa wicked cool Property Builder and everything.  So just build up your look and feel and whatnot just like you normally would.

I'm going to skip down to the part where we add the sorting capability.  I'd probably be wasting my time going through loading it and whatnot, so here goes:

First, make sure you have a Protected WithEvents declaration on the page:
Protected WithEvents Link9Grid As Link9.customControls.reversableGrid

You gotta have yourself an event handler to handle the sort command of the Link9Grid, right?  So it might (and I stress might) look like this:

Protected Sub HandleSorting(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles Link9Grid.SortCommand

  'We're telling the grid what the user clicked here
  Link9Grid.sortField = e.sortExpression  

End Sub

Now I prefer to handle things this way because it keeps my code looking real nice.  Notice I wired the event handler using the Handles keyword on the function, rather than adding it declaratively to the Link9Grid.  bindGrid() on my pages typically look like this:

private sub bindGrid()
  Link9Grid.datasource = returnDataView()
end sub

Again, we abstract the functionality into a very easy to follow hierarchy.  So that returnDataView() is where the magic happens.  I'll assume you have already created a dataset from your datastore, and you have called it 'ds'.  This dataset has 1 table in it.  Here's how to apply our autoreverse function:

 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
dim dv as new dataview(ds.tables(0).defaultView)
dv.sort = Link9Grid.sortExpression
return dv
 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Look, I apologize that this FAQ got a little long winded, but I think we covered quite a bit here, and just so we don't confuse things...  if you already have a page using a datagrid, then replace that datagrid with this one, and when you apply your sort, just:

dv.sort = Link9Grid.sortExpression

and you're done.  

The proof is in the pudding, people.  I don't know how we ever developed with anything else.

happy coding!  
Paul Prewett

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close