Solved

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

Posted on 2008-06-10
9
1,066 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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