Link to home
Start Free TrialLog in
Avatar of davidcahan
davidcahanFlag for United States of America

asked on

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

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

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of davidcahan

ASKER

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.
String what together?  I don't understand what you mean by that exactly.
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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
beautiful...that was EXACTLY the kind of "starting off" information i was looking for.  
great advice as always from TheLearnedOne.  After his suggestions I was able to complete the solution