This is a migrated thread and some comments may be shown as answers.

Hiding and Displaying Grid columns based on user input

1 Answer 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Milan Gurung
Top achievements
Rank 1
Milan Gurung asked on 04 Dec 2008, 06:58 PM
Hi Everyone,

I got a problem in Rad Grid - 

Scenario: I have got a grid that displays 5-10 columns. In the same page I have provided user with checkboxes so that they can either hide or display the columns based on whether the checkboxes are selected or not. There is no problem hiding column but when displaying back the hidden column, I have to call rebind() method of radGrid to display the column back. Due to this, collasping feature is not working. All this is performed in PreRender method which is called everytime when the checkbox is ticked or unticked (auto postback = true for checkbox)

Please any sort of help is appreciated. 


Code snippet:
Protected Sub gridExpenditure_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridExpenditure.PreRender
        Dim availalbleCols As New List(Of String)
        availalbleCols.Add("Business")
        availalbleCols.Add("Brand")
        availalbleCols.Add("ConsumerActivityName")
        availalbleCols.Add("ConsumerActivityID")
        availalbleCols.Add("Vendor")
        availalbleCols.Add("Supplier")
        availalbleCols.Add("Media")
        availalbleCols.Add("MarketingPlanName")
        availalbleCols.Add("Material")
        availalbleCols.Add("CostElement")

        Dim selectedCols As New List(Of String)
        Dim unSelectedCols As New List(Of String)
        Dim i As Integer
        For i = 0 To cbAvailableCols.Items.Count - 1
            If cbAvailableCols.Items(i).Selected = False Then
                unSelectedCols.Add(cbAvailableCols.Items(i).Value)
            Else
                selectedCols.Add(cbAvailableCols.Items(i).Value)
            End If
        Next
        'Set visible to false for missing Columns only
        If Not unSelectedCols.Count = 0 Then
            Dim gridCol As GridColumn
            For Each gridCol In gridExpenditure.MasterTableView.RenderColumns
                Dim colName As String
                For Each colName In unSelectedCols
                    If gridCol.UniqueName = colName Then
                        gridCol.Visible = False
                    End If
                Next
            Next
        End If

        'Set visible to true for rest of the columns
        If Not selectedCols.Count = 0 Then
            Dim gridCol As GridColumn
            For Each gridCol In gridExpenditure.MasterTableView.RenderColumns
                Dim colName As String
                For Each colName In selectedCols
                    If gridCol.UniqueName = colName Then
                        gridCol.Visible = True
                        'Calling rebind avoids collapse feature but it is needed when user wants to add column :(
                        'Need to fix this somehow...
                        gridExpenditure.Rebind()
                    End If
                Next
            Next
        End If

    End Sub



Thanks a lot.

Sincerely,
Milan G

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 05 Dec 2008, 05:48 PM
Hello Milan,

Please test the attached example and let me know whether it helps.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Milan Gurung
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or