Solved

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

Posted on 2008-06-10
9
1,074 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
ID: 21759227
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
ID: 21759692
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
ID: 21760245
String what together?  I don't understand what you mean by that exactly.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:davidcahan
ID: 21760993
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 21761957
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
ID: 21762448
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
ID: 21762508
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
ID: 21762586
beautiful...that was EXACTLY the kind of "starting off" information i was looking for.  
0
 

Author Closing Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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