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

Persisting Grid Settings - Save on Column Show/Hide

3 Answers 218 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 07 Jan 2013, 07:55 PM
I have a RadGrid that has several columns.  (18 to be precise) though only 6 of these columns display by default. The rest are set to 'display=false'.   I'm trying to make it show that when a user changes what columns are displayed that change is persisted through their experience using sessions so that if they navigate away and come back to the page it will retain the changes they made.

I am following the instructions provided:  http://demos.telerik.com/aspnet-ajax/grid/examples/programming/savinggridsettingsonperuserbasis/defaultcs.aspx on the demo however I am having issues with modifying this so that it calls my SaveSettings method.

Is there a way to have it so that when the user hides or shows a column that it calls the SaveSettings method?
ASPX
<telerik:RadGrid ID="gvDash" runat="server" GridLines="None" AutoGenerateColumns="false"
            DataSourceID="dsDashboard" PageSize="15" Width="100%" ShowStatusBar="true"
            AllowPaging="true" AllowSorting="true">
            <MasterTableView DataKeyNames="FileID" CommandItemDisplay="Top" AllowMultiColumnSorting="true" EnableHeaderContextMenu="true" EditMode="PopUp">
                <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToWordButton="true" ShowExportToExcelButton="true" />
                <NoRecordsTemplate>
                    <em>No Records</em>
                </NoRecordsTemplate>
                <AlternatingItemStyle CssClass="alt-row" />
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="Options" Groupable="false" AllowFiltering="false" HeaderText="Options">
                        <ItemTemplate>
                            <asp:HyperLink  ID="EditLink" runat="server" Text="Edit"></asp:HyperLink>
                            <asp:LinkButton ID="btnView" runat="server" Text="View" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn UniqueName="FileNo" DataField="FileNo" HeaderText="File Number" SortExpression="FileNo" Groupable="false" />
                    <telerik:GridBoundColumn UniqueName="DisplayName" DataField="DisplayName" HeaderText="Vessel" SortExpression="DisplayName" />
                    <telerik:GridBoundColumn UniqueName="VesselStatusName" DataField="VesselStatusName" HeaderText="Status" SortExpression="VesselStatusName" />
                    <telerik:GridBoundColumn UniqueName="OfficeName" DataField="OfficeName" HeaderText="Office Location" SortExpression="OfficeName" />
                    <telerik:GridCheckBoxColumn UniqueName="AddedToQB" DataField="AddedToQB" HeaderText="Vsl Added QB" SortExpression="AddedToQB" />
                    <telerik:GridBoundColumn UniqueName="VesselSailed" DataField="VesselSailed" HeaderText="Sail Date" SortExpression="VesselSailed" DataFormatString="{0:M/d/yyyy HH:mm}" />
                    <telerik:GridCheckBoxColumn UniqueName="SailDateQB" DataField="SailDateQB" HeaderText="Sail Date Added QB" SortExpression="SailDateQB" />
                    <telerik:GridBoundColumn UniqueName="CargoName" DataField="CargoName" HeaderText="Cargo" SortExpression="CargoName" />
                    <telerik:GridTemplateColumn UniqueName="PayingPrinci" HeaderText="Paying Principal" GroupByExpression="PayingPrinciName Group By PayingPrinciName" SortExpression="PayingPrinciName">
                        <ItemTemplate>
                            <asp:Label ID="lblPayingPrinci" runat="server" Text='<%# Bind("PayingPrinciName") %>'></asp:Label>
                            <br /><asp:Label ID="lblPayingPrinciAddress" CssClass="label-note" runat="server" Text='<%# Bind("PayingPrinciAddress") %>'></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn UniqueName="PDAAmnt" DataField="PDAAmnt" HeaderText="PDA AMT" SortExpression="PDAAmnt" DataFormatString="{0:c}" />
                    <telerik:GridBoundColumn UniqueName="RECVDAmnt" DataField="RECVDAmnt" HeaderText="PDA AMT Rcvd" SortExpression="RECVDAmnt" DataFormatString="{0:c}" />
                    <telerik:GridBoundColumn UniqueName="PerformaDateRcvd" DataField="PerformaDateRcvd" HeaderText="PDA Date Rcvd" SortExpression="PerformaDateRcvd" DataFormatString="{0:M/d/yyyy}" />
                    <telerik:GridBoundColumn UniqueName="CTMAmnt" Display="false" DataField="CTMAmnt" HeaderText="CTM AMT" SortExpression="CTMAmnt" DataFormatString="{0:c}" />
                    <telerik:GridBoundColumn UniqueName="CTMAmntRcvd" Display="false" DataField="CTMAmntRcvd" HeaderText="CTM AMT Rcvd" SortExpression="RECVDAmnt" DataFormatString="{0:c}" />
                    <telerik:GridBoundColumn UniqueName="CTMAmntDate" Display="false" DataField="CTMAmntDate" HeaderText="CTM Date Rcvd" SortExpression="PerformaDateRcvd" DataFormatString="{0:M/d/yyyy}" />
                    <telerik:GridBoundColumn UniqueName="PortName" DataField="PortName" HeaderText="Port" SortExpression="PortName" />
                    <telerik:GridBoundColumn UniqueName="AccountingNotes" Display="false" DataField="AccountingNotes" HeaderText="Comments" />
                </Columns>
                </MasterTableView>
            <ClientSettings AllowColumnsReorder="true" AllowColumnHide="true">
                <Resizing AllowColumnResize="true" AllowResizeToFit="true" />
            </ClientSettings>
        </telerik:RadGrid>

Code Behind
Private Sub LoadSettings()
        If Not IsNothing(Session("Acc.Funds.GridSettings")) Then
            Dim LoadPersister As New GridSettingsPersister(gvDash)
            Dim settings As String = DirectCast(Session("Acc.Funds.GridSettings"), String)
            LoadPersister.LoadSettings(settings)
        End If
End Sub   
Private Sub SaveSettings()
        Dim SavePersister As New GridSettingsPersister(gvDash)
        Session("Acc.Funds.GridSettings") = SavePersister.SaveSettings()
    End Sub

3 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 10 Jan 2013, 03:32 PM
Hello Ron,
 
A possible approach is when you hide the column to save the setting in the same event. I prepared a small sample and attached it to this forum post. When you click on Hide/Show column button the column will hide/show and will be saved in the session. Then if you want you could load the session and this column will be visible/hidden.
Please give this example a try and let me know about the result.

Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Ron
Top achievements
Rank 1
answered on 14 Jan 2013, 03:47 PM
Thanks for the prompt response however the solution you have offered me still requires a button press outside of the columns selection and does not fit the needs I have.  It's similar to the one I came up with myself that allows them to save their current settings by clicking a 'Save Settings' button.

I guess since the column hide/show is a client-side function it isn't something I can capture on a server-side method.
0
Kostadin
Telerik team
answered on 17 Jan 2013, 01:40 PM
Hi Ron,

I am not completely sure I understand when exactly you want to save the settings. If you want to save them every time when you hide a column from the context menu you could hook OnColumnHidden client event and specify a command which will be executed. Check out the following code snippet.
JavaScript:
    function ColumnHidden(sender, args) {
    var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
    masterTable.fireCommand("SaveUserSettings", "");
}
VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
 
    If e.CommandName = "SaveUserSettings" Then
        Dim SavePersister As New GridSettingsPersister(RadGrid1)
        Session("user") = SavePersister.SaveSettings()
    End If
 
End Sub

Additionally I modified the project from my previous reply and attached it again.

All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Ron
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Ron
Top achievements
Rank 1
Share this question
or