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

To remember the settings done for Column hiding/unhiding and column resizing

9 Answers 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sudha
Top achievements
Rank 1
Sudha asked on 15 Nov 2011, 07:21 AM
Hi,
We have a requirement where once the user hides or unhides the column OR resize  the column width, these settings must be remembered for that user.
Kindly let us know if this can this achieved.

Thanks,
Sudha.

9 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Nov 2011, 07:28 AM
0
Sudha
Top achievements
Rank 1
answered on 15 Nov 2011, 08:04 AM
Thanks for the reply Jayesh. I tried this. It worked. But the settings will  not be saved when all the browsing windows are closed.
In the link that you provided , I followed the following steps.
1) save the settings for user 3.
2) close the browser.
3) Open the link and selected user 3 from the drop down list. The load settings button was not enabled.

Can this settings be remembered for atleast 3 months time for that user ?
0
Sudha
Top achievements
Rank 1
answered on 15 Nov 2011, 09:24 AM
Thanks for the reply Jayesh. I tried this. It worked. But the settings will  not be saved when all the browsing windows are closed.
In the link that you provided , I followed the following steps.
1) save the settings for user 3.
2) close the browser.
3) Open the link and selected user 3 from the drop down list. The load settings button was not enabled.

Can this settings be remembered for atleast 3 months time for that user ?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Nov 2011, 09:34 AM
Hello Sudha,

Ffor that you have to store this settings in DB or any other files.

-- you can save persisting of the grid in DB  in grid_prerender event.
-- if user logged in you can get persisting setting from DB and assign to RadGrid in Page_Laod event.

Thanks,
Jayesh Goyani
0
Sudha
Top achievements
Rank 1
answered on 15 Nov 2011, 10:05 AM
Thank you so much.
Could you please send us the code part to save the changes in DB ?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Nov 2011, 11:43 AM
Hello Sudha,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Persistsettingdemo.aspx.cs"
    Inherits="Persistsettingdemo" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
            Skin="Vista" AllowSorting="true" AllowFilteringByColumn="true"
            ShowGroupPanel="true" onprerender="RadGrid1_PreRender">
            <MasterTableView DataKeyNames="ID1">
                <CommandItemSettings ShowExportToExcelButton="true" />
                <Columns>
                    <telerik:GridBoundColumn DataField="ID1" HeaderText="ID1" UniqueName="ID1">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID2" HeaderText="ID2" UniqueName="ID2">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings AllowDragToGroup="true">
                <Resizing AllowColumnResize="true" />
            </ClientSettings>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Data.SqlClient;
 
public partial class Persistsettingdemo : System.Web.UI.Page
{
    public static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Jayesh.goyani\fileupload\App_Data\PersistDatabase.mdf;Integrated Security=True;User Instance=True");
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
                new { ID1 = 1, ID2 = 2},
                new { ID1 = 4, ID2 = 5}
                
            };
        RadGrid1.DataSource = data;
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlCommand cmd = new SqlCommand();
            // Here assume that USERID is 1.
            cmd.CommandText = "SELECT  UserID, PersistString, GridId FROM  PersistTable WHERE UserID = 1 AND GridId=1";
            cmd.Connection = con;
 
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string settings = reader["PersistString"].ToString();
                    GridSettingsPersister LoadPersister = new GridSettingsPersister(RadGrid1);
                    LoadPersister.LoadSettings(settings);
                    RadGrid1.Rebind();
                }
                reader.Close();
            }
 
            finally
            {
                con.Close();
            }
        }
        else
        {
            bool IsExists = false;
 
 
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT  UserID, PersistString, GridId FROM  PersistTable WHERE UserID = 1 AND GridId=1";
            cmd.Connection = con;
 
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    IsExists = true;
                }
                reader.Close();
 
                if (IsExists)
                {
                    //update existing entry
                    GridSettingsPersister SavePersister = new GridSettingsPersister(RadGrid1);
                    string updateString = @" UPDATE PersistTable SET PersistString='" + SavePersister.SaveSettings() + "' WHERE UserID = 1 AND GridId = 1  ";
 
                    SqlCommand cmdinsert = new SqlCommand(updateString, con);
                    cmdinsert.ExecuteNonQuery();
                }
                else
                {
                    // insert a new entry
                    GridSettingsPersister SavePersister = new GridSettingsPersister(RadGrid1);
                    string insertString = @" insert into PersistTable ( UserID, PersistString, GridId) values (1,'" + SavePersister.SaveSettings() + "',1)";
 
                    SqlCommand cmdinsert = new SqlCommand(insertString, con);
                    cmdinsert.ExecuteNonQuery();
 
 
                }
            }
            finally
            {
                con.Close();
            }
        }
    }
 
 
}

I do this thing in grid_prerender event you can also do this thing in Page_Prerender any other. as per your requirement.

Thanks,
Jayesh Goyani
0
Sudha
Top achievements
Rank 1
answered on 18 Nov 2011, 07:37 AM
Thanks a lot Jayesh. It worked.
But still have one problem with this. I am not able to save the width of the column after resizing. Could you please help us in solving this issue as early as possible.

Thanks in advance,
Sudha.
0
Sudha
Top achievements
Rank 1
answered on 18 Nov 2011, 07:39 AM

Thanks a lot Jayesh. It worked.
But still have one problem with this. I am not able to save the width of the column after resizing. Could you please help us in solving this issue as early as possible.

Thanks in advance,
Sudha.

0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Nov 2011, 12:32 PM
Hello Sudha,

we are not able to save the width of the column because we are resizing the column on client so.

To achieve this functionality follow the below steps.
-- using "OnColumnResized" client event store all columns width in any hidden field.
    like : column1,124px:column2,148px.........etc
-- when you store the grid persisting setting at that time also set this hidden field value in DB.

Check below link for how to access all columns using JS.
http://www.telerik.com/community/forums/aspnet-ajax/grid/setting-a-maximum-size-of-a-radgrid-column.aspx

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Sudha
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Sudha
Top achievements
Rank 1
Share this question
or