Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1109
  • Last Modified:

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

0
davidcahan
Asked:
davidcahan
  • 5
  • 4
2 Solutions
 
Bob LearnedCommented:
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
 
davidcahanAuthor Commented:
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
 
Bob LearnedCommented:
String what together?  I don't understand what you mean by that exactly.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
davidcahanAuthor Commented:
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
 
Bob LearnedCommented:
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
 
davidcahanAuthor Commented:
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
 
Bob LearnedCommented:
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
 
davidcahanAuthor Commented:
beautiful...that was EXACTLY the kind of "starting off" information i was looking for.  
0
 
davidcahanAuthor Commented:
great advice as always from TheLearnedOne.  After his suggestions I was able to complete the solution
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now