Storing Multiple Grid settings in Database via Profile

Thread is closed for posting
10 posts, 1 answers
  1. 3395D7E4-35C1-4561-A99D-CC88C6DDE9FF
    3395D7E4-35C1-4561-A99D-CC88C6DDE9FF avatar
    165 posts
    Member since:
    Oct 2004

    Posted 01 Feb 2007 Link to this post

    Requirements

    RadGrid for ASP .NET version

    RadControls for ASP .NET AJAX version
    4.x and later


    2008.1.415 and later
    .NET version

    2.x and later
    Visual Studio version

    2005 and later
    Programming language

    C#
    Browser support

    all supported by RadGrid for ASP .NET


    all browsers supported by RadControls for ASP .NET AJAX



    PROJECT DESCRIPTION

    To enable your users to have their own personalised settings each time they re-visit pages with grids on follow the instructions below:

    1. Run the attached site.
    2. You will see in the web.config that you need to enter connection settings to retrieve data. 
    3. Look at the profile settings, you will see that you have the serializeAs=Binary which is important. The name of the Profile property is entirely up to you but would make sense to call it the same as the class in which the settings are read from, as you can see it is called Pets.
    4. Open the GridSettings.cs file. You will see that there is a GridSettings class that loads/saves the grid settings. I have added the functionality to load/save column visibility. (This will be developed further so a context menu on the grid is populated with columns the OnClick will set the column visibility)
    5. Look at the Pets class. Here we will store grid settings in a hash table ie/the grid name and the grid settings.
    6. Open Default.aspx.cs. There are 2 grids and 2 buttons, each button will set the 1st column invisible. Look at the Page_Init and the Render events. In Page_Init you will see a check for if (Profile.Pet.Names.Count > 0), Names is the hashtable that stores the settings for the grids. If there is data in there it will check for a key that represents your grid. If so it will create an instance of the grid settings class and load the settings for the grid.

       NOTE: When using AutoGeneratedColumn grids you will have to investigate further where to call the LoadSettings method from. I have explained why in the source code but basically Grid.AutoGeneratedColumns is empty in Page_Init.
    7. Now look at the Render event. This comes after the Page_Init and any postbacks, for example, setting a grid column invisible. The cycle will be user clicks button, the settings are loaded, the button click event is called making a column invisible, then the render event is called to save those settings. Try logging out and coming back in or changing user. You will see that your settings have been saved else if you log in as a different user the columns are visible.
    8. Good Luck. For further information see here
  2. C7498A83-7E2E-418C-8791-93EF573A7569
    C7498A83-7E2E-418C-8791-93EF573A7569 avatar
    9934 posts
    Member since:
    Nov 2016

    Posted 06 Feb 2007 Link to this post

    Hello Jonathan,

    Thank you for the time you dedicated to assemble this sample application and provide a detailed description about the implementation. This will surely help other community members who want to persist the grid settings depending on the logged user even when he/she ends its Session and then log in again. 5000 telerik points goes to your account for the involvement.

    Additionally, you receive another 1000 points for extending the example with some extra code (including context menu which switches the columns visibility). I will paste below the content from the forum post which discusses this matter in our public forums:

    I have added the functionality to have a right click context menu with the columns and be able to set them visible or not. After LoadSettings on Pre_Init call call AddColumnsToMenu then on Render after SaveSettings call AddColumnsToMenu. I have added code to change colour depending on visibility so the user knows.

    NOTE: Even though the code goes through the colour changing code it always seems to be the same colour as when the page 1st rendered. I am moving on to look at checkboxes rather than colours so hopefully that will help.

    Please add this to my KB article.

    Here is the code

    private void AddColumnsToMenu()  
        {  
            RadMenuItem item = ExportMenu.Items.FindItemByText("Columns");  
            if (item != null)  
            {   
                item.Items.Clear();  
                ExportMenu.Items.Remove(item);  
            }  
     
            RadMenuItem visibleMenuItem = new RadMenuItem("Columns");  
            foreach (GridColumn column in dgReports.MasterTableView.Columns)  
            {  
                if (column.Visible)  
                {  
                    RadMenuItem menuItem = new RadMenuItem(column.HeaderText);  
                    menuItem.Value = ((GridBoundColumn)column).DataField;  
                    menuItem.ForeColor = Color.Red;  
                    visibleMenuItem.Items.Add(menuItem);  
                }  
                else 
                {  
                    RadMenuItem menuItem = new RadMenuItem(column.HeaderText);  
                    menuItem.Value = ((GridBoundColumn)column).DataField;  
                    menuItem.ForeColor = Color.Yellow;  
                    visibleMenuItem.Items.Add(menuItem);  
                }  
            }  
            if (visibleMenuItem.Items.Count > 0)  
            {  
              ExportMenu.Items.Add(visibleMenuItem);  
            }  
        }    
     


    Thank you once again for sharing your solution with us, and keep posting.

    Best wishes,

    Stephen

    the telerik team

  3. 7DF07AD8-E3B2-4FF7-9E62-9D03B7D0C183
    7DF07AD8-E3B2-4FF7-9E62-9D03B7D0C183 avatar
    50 posts
    Member since:
    Dec 2005

    Posted 14 May 2007 Link to this post

    Hi Colin and Stepen,

    When I upgraded to Q1 2007 the serialization failed on the version number.  Is the serialization storing the dll binary version?  Did I do something wrong or is there a way around this so when I upgrade Telerik it does not blow away each users previous settings.

    Thanks,
    David
  4. C7498A83-7E2E-418C-8791-93EF573A7569
    C7498A83-7E2E-418C-8791-93EF573A7569 avatar
    9934 posts
    Member since:
    Nov 2016

    Posted 15 May 2007 Link to this post

    Hi David,

    You should avoid the serialization error specifying assemblyBinding clause in the Web.config file of your web site, for example:

    <configuration> 
        <appSettings/> 
        <connectionStrings> 
            <add name="NorthwindConnectionString" connectionString="Data Source=(local);Initial Catalog=Northwind;uid=sa;pwd=sa;" providerName="System.Data.SqlClient"/>  
        </connectionStrings> 
      <runtime> 
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
          <dependentAssembly> 
            <assemblyIdentity name="RadGrid.Net2" publicKeyToken="ec87bc2939ec45df" culture="neutral"/>  
            <bindingRedirect oldVersion="4.5.1.0" newVersion="4.6.0.0"/>  
          </dependentAssembly> 
        </assemblyBinding> 
      </runtime> 
    -------------  
    </configuration> 

    Once the project is compiled successfully, you can safely remove this code block if needed. I have tested the approach locally to verify the proper behavior.

    Best regards,
    Stephen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. 7DF07AD8-E3B2-4FF7-9E62-9D03B7D0C183
    7DF07AD8-E3B2-4FF7-9E62-9D03B7D0C183 avatar
    50 posts
    Member since:
    Dec 2005

    Posted 15 May 2007 Link to this post

    Excellent - Thanks.  I will give it a try.

    David
  6. 6EC5B7E0-5F20-416C-A21B-909C13E03A1C
    6EC5B7E0-5F20-416C-A21B-909C13E03A1C avatar
    80 posts
    Member since:
    May 2007

    Posted 18 Jul 2007 Link to this post

    Hi

    I try to integrate this into my project having a grid with autogenerated columns. My project is in VB as I have to use Telerik Code Converter www.codechanger.com to change the gridSettings class to VB. I noticed all the castings were changed to VB's DirectCast. After fixing all the syntax errors I could not make it work. An exception is always thrown inside function SaveSettings of class GridSettings.cs reporting either System.NullReferenceException or System.InvalidCastException (unable to cast object of type System.Web.UI.Pair[] to System.Object) at the following lines

    For Each expression As GridGroupByExpression In groupByExpressions
        groupExpressions(count) = (DirectCast(expression, IStateManager)).SaveViewState()
        System.Math.Max(System.Threading.Interlocked.Increment(count), count - 1)
    Next

    It appears that the code could not load the previous settings saved in user profile. I have to proceed to delete everything inside aspnet_Profile table to make the page load properly again; but the moment I refresh it, the code attemps to load the previous settings and will again fail. However, the sample code provided works as expected.

    Has anyone successfully tried this with an autogenerated column grid before?
  7. 6AB3838B-B392-4EDE-97F0-B3F5916D8900
    6AB3838B-B392-4EDE-97F0-B3F5916D8900 avatar
    11100 posts
    Member since:
    Jan 2017

    Posted 19 Jul 2007 Link to this post

    Hello mdanh2002,

    You can find VB.NET version of the code in our help article here:

    http://www.telerik.com/help/radcontrols/prometheus/?grdSavingSettingsOnPerUserBasis.html
    http://www.telerik.com/help/aspnet/grid/?grdSavingSettingsOnPerUserBasis.html

    I hope it will help you convert the C# implementation in VB.NET language successfully.

    Best regards,
    Vlad
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. Answer
    20A70A9E-F8A9-4648-A697-5CF602B0C136
    20A70A9E-F8A9-4648-A697-5CF602B0C136 avatar
    338 posts
    Member since:
    Jun 2008

    Posted 31 Jul 2008 Link to this post

    In the GridSettings the following error is coming up:

    Method must have a return type:
    public GridSettings(RadGrid gridInstance)  
      {  
        this.gridInstance = gridInstance;  
      } 
  9. C7498A83-7E2E-418C-8791-93EF573A7569
    C7498A83-7E2E-418C-8791-93EF573A7569 avatar
    9934 posts
    Member since:
    Nov 2016

    Posted 31 Jul 2008 Link to this post

    Hi Mike,

    The examples from this thread should be operational without any code changes in the main logic. If you experience problems with your custom implementation, please submit a formal support ticket with test demo attached to it. Thus we will be able to advice you for your particular scenario.

    Best regards,
    Stephen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. CF04F382-A994-4C3A-93EE-FED28AC8E0EC
    CF04F382-A994-4C3A-93EE-FED28AC8E0EC avatar
    2002 posts
    Member since:
    Mar 2023

    Posted 21 Aug 2009 Link to this post

    Hello,

    The original Code Library project demonstrates saving per-user settings for a single page. If you have multiple pages with identically named RadGrids that you want to save, you will need to extend the project a.

    First, you need to make sure that no matter that two RadGrids have the same ID in two different pages, they need to be referenced by different names in the Pet.Names hashtable. A suggestion would be to use a name formed by appending the control ID to the Page URL:

    C#
    private string GetUniqueProfileKey(Control control)  
    {  
        return control.ID != "" ? Request.Url.LocalPath + "_" + control.ID : "";  
    }  

    VB.NEt
    Private Function GetUniqueProfileKey(control As Control) As String  
        Return If(control.ID <> "", Request.Url.LocalPath + "_" + control.ID, "")  
    End Function  

    You can now use the above function to get a unique ID for the Hashtable containing your settings.

    The second step is to make sure all settings are saved to one and the same object. If you notice, the overriden Render event re-initializes a new Pet object and assigns it to Profile.Pet. This means that every time  a page is rendered, the Profile.Pet object will contain only the settings of the rendered page. All previous settings will be removed.

    For this reason, we need to add settings up to the same Pet object on all pages. Simply remove

    Profile.Pet = New Pet() 

    so that you always have the same Pet object referenced.

    Lastly, a small fix for the Pet class, to make sure we update the settings for an existing key, instead of trying to add a new key-value pair:

    Was:
    public void SavePetNames(RadGrid Grid, string GridName) 
        GridSettings settings = new GridSettings(Grid); 
        string settingvalue = settings.SaveSettings(); 
        _names.Add(GridName, settingvalue); 
     

    Is:
    public void SavePetNames(RadGrid Grid, string GridName) 
        GridSettings settings = new GridSettings(Grid); 
        string settingvalue = settings.SaveSettings(); 
        _names[GridName] = settingvalue; 
     


    Regards,
    Veli
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.