Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I add auto-reverse to my datagrid?

Extensability

How can I add auto-reverse to my datagrid?

by  link9  Posted    (Edited  )
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:

Code:
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)
            MyBase.Render(output)
        End Sub

        Public ReadOnly Property sortExpression() As String
            Get
                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
            Get
                Dim o As Object = ViewState.Item("sortField")
                Dim output As String
                If o Is Nothing Then
                    output = String.Empty
                Else
                    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
            Get
                Dim o As Object = ViewState.Item("sortAscending")
                Dim output As String
                If o Is Nothing Then
                    output = True
                Else
                    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:
Code:
<%@ 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:
Code:
<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:
Code:
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:

Code:
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  
  bindGrid()

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:
Code:
private sub bindGrid()
  link9Grid.datasource = returnDataView()
  link9Grid.dataBind()
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:
Code:
 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
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

[img http://www.paul-jessica.com/images/penny1.gif][img http://www.paul-jessica.com/images/penny1.gif]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top