Solved

Help Building Custom "Extender" Class for RadGrid (DataGrid)

Posted on 2008-06-10
9
1,053 Views
Last Modified: 2010-04-21
I'm trying to take code that i've obtained from Telerik, modified and gotten working on the actual page the Grid appears on and instead create a class so that whenever i need any RadGrid to have the "extended" functionionality, I can simply create an instance of the class and pass my grid into it.

I'M TOTALLY LOST!!!!

I have no clue how to string the subroutines together, nor do i have any clue how to ultimately link them back to the grids databound and itemcommand events.  

I'm trying to build a "master" subroutine called PersistState that will accept in the Grid Object, run through the necessary subroutines, and "do its thing" (for lack of a better phrase).  Obviously I'm at the very start and I haven't changed alot of the code from how it was written when this functionality appeared directly on the page with the Grid Instance

Please help me!!!
Imports Microsoft.VisualBasic
 

Public Class RadGridExtender
 

    Private _ordersExpandedState As Hashtable

    Private _selectedState As Hashtable

    Private ReadOnly Property ExpandedStates() As Hashtable

        Get

            If Me._ordersExpandedState Is Nothing Then

                _ordersExpandedState = TryCast(Me.Session("_ordersExpandedState"), Hashtable)

                If _ordersExpandedState Is Nothing Then

                    _ordersExpandedState = New Hashtable()

                    Me.Session("_ordersExpandedState") = _ordersExpandedState

                End If

            End If
 

            Return Me._ordersExpandedState

        End Get

    End Property
 
 

    Public Sub PersistState(ByVal Grid As Telerik.WebControls.RadGrid)
 

    End Sub
 
 

    'Clear the state for all expanded children if a parent item is collapsed 

    Private Sub ClearExpandedChildren(ByVal parentHierarchicalIndex As String)

        Dim indexes As String() = New String(Me.ExpandedStates.Keys.Count - 1) {}

        Me.ExpandedStates.Keys.CopyTo(indexes, 0)

        For Each index As String In indexes

            'all indexes of child items 

            If index.StartsWith(parentHierarchicalIndex + "_") OrElse index.StartsWith(parentHierarchicalIndex + ":") Then

                Me.ExpandedStates.Remove(index)

            End If

        Next

    End Sub

    'Save/load selected states Hash from the session 

    'this can also be implemented in the ViewState 

    Private ReadOnly Property SelectedStates() As Hashtable

        Get

            If Me._selectedState Is Nothing Then

                _selectedState = TryCast(Me.Session("_selectedState"), Hashtable)

                If _selectedState Is Nothing Then

                    _selectedState = New Hashtable()

                    Me.Session("_selectedState") = _selectedState

                End If

            End If
 

            Return Me._selectedState

        End Get

    End Property

    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)

        'save the expanded/selected state in the session 

        If e.CommandName = RadGrid.ExpandCollapseCommandName Then

            'Is the item about to be expanded or collapsed 

            If Not e.Item.Expanded Then

                'Save its unique index among all the items in the hierarchy 

                Me.ExpandedStates(e.Item.ItemIndexHierarchical) = True

            Else

                'collapsed 

                Me.ExpandedStates.Remove(e.Item.ItemIndexHierarchical)

                Me.ClearExpandedChildren(e.Item.ItemIndexHierarchical)

            End If

        ElseIf e.CommandName = RadGrid.SelectCommandName Then

            'Is the item about to be selected 

            'Save its unique index among all the items in the hierarchy 

            Me.SelectedStates(e.Item.ItemIndexHierarchical) = True

        ElseIf e.CommandName = RadGrid.DeselectCommandName Then

            'Is the item about to be deselected 

            Me.SelectedStates.Remove(e.Item.ItemIndexHierarchical)

        End If

    End Sub

    Protected Sub RadGrid1_DataBound(ByVal sender As Object, ByVal e As EventArgs)

        'Expand all items using our custom storage 

        Dim indexes As String() = New String(Me.ExpandedStates.Keys.Count - 1) {}

        Me.ExpandedStates.Keys.CopyTo(indexes, 0)
 

        Dim arr As New ArrayList(indexes)

        'Sort so we can guarantee that a parent item is expanded before any of 

        'its children 

        arr.Sort()
 

        For Each key As String In arr

            Dim value As Boolean = CBool(Me.ExpandedStates(key))

            If value Then

                rgHotels.Items(key).Expanded = True

            End If

        Next
 

        'Select all items using our custom storage 

        indexes = New String(Me.SelectedStates.Keys.Count - 1) {}

        Me.SelectedStates.Keys.CopyTo(indexes, 0)
 

        arr = New ArrayList(indexes)

        'Sort to ensure that a parent item is selected before any of its children 

        arr.Sort()
 

        For Each key As String In arr

            Dim value As Boolean = CBool(Me.SelectedStates(key))

            If value Then

                rgHotels.Items(key).Selected = True

            End If

        Next

    End Sub
 

End Class

Open in new window

0
Comment
Question by:davidcahan
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you trying to inherit from the RadGrid, and extend it?  If you so, I would start with something like this:

Public Class SpecialRadGrid
     Inherits Telerik.WebControls.RadGrid
0
 

Author Comment

by:davidcahan
Comment Utility
I didn't think of it like that.   I was originally just trying to build a class that will allow me to "persist" the state of the grid.  I wanted it in a class so that any Grid anywhere in my site could call this class.  I also wanted to "hide" the complicated code and keep the code on the main page "cleaner".

If i extended the class i could call my own custom Grid.

I'd still like to know, by looking at my code, how i go about passing the Grid object into the class and then string all the subroutines together.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
String what together?  I don't understand what you mean by that exactly.
0
 

Author Comment

by:davidcahan
Comment Utility
well, i'd like to know how to pass the Grid object into my master subroutine, PersistState(), and then from there pass it from subroutine to subroutine in order to acheive the desired results.  Basically, I'm lost as to how to take the code, which originally appears directly on the page that the grid instance is on and turn it into a stand alone class.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
Try this:

    Private m_grid As Telerik.WebControls.RadGrid = Nothing

    Public Sub PersistState(ByVal Grid As Telerik.WebControls.RadGrid)
         m_grid = Grid
    End Sub

Then, use the reference for m_grid inside the class instance.
0
 

Author Comment

by:davidcahan
Comment Utility
K...let me try starting with that.  I'll play with it for a while and see if i can at least get it further than i have it right now (which is basically no where), before I ask more questions.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
Comment Utility
If you want handle events for the RadGrid, you need a slight adjustment:

    Private WithEvents m_grid As Telerik.WebControls.RadGrid = Nothing

    Public Sub PersistState(ByVal Grid As Telerik.WebControls.RadGrid)
         m_grid = Grid
    End Sub

    Protected Sub RadGrid1_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles m_Grid.DataBound

0
 

Author Comment

by:davidcahan
Comment Utility
beautiful...that was EXACTLY the kind of "starting off" information i was looking for.  
0
 

Author Closing Comment

by:davidcahan
Comment Utility
great advice as always from TheLearnedOne.  After his suggestions I was able to complete the solution
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now