Save/Load scrollbar position and expanded state of groups

6 posts, 1 answers
  1. Johannes
    Johannes avatar
    66 posts
    Member since:
    Dec 2012

    Posted 22 Aug 2013 Link to this post

    Is there any build-in solution to save and restore the position of RadGridView's VerticalScrollbar?
    Is there any build-in solution to save and restore the expand/collapse state of displayed groups?

    I use RadGridView's SaveLayout() and LoadLayout() methods and it seems the values mentioned above are not being persisted to the layout file.
  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2831 posts

    Posted 27 Aug 2013 Link to this post

    Hello Johannes,

    Thank you for writing.

    There is no build-in logic for restoring the position of the scrollbar or to save the expanded/collapsed rows. However I have prepared a small sample project to demonstrate how you can do this manually. In the sample I use two buttons one for saving the current view and one for restoring it. In the first button click event I am saving the scroll position and also I am saving the rows state in a bool array. In the restore button click event I am just restoring the previous states.

    I hope this will be useful. Should you have further questions, I would be glad to help.

    Regards,
    Dimitar
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. Johannes
    Johannes avatar
    66 posts
    Member since:
    Dec 2012

    Posted 27 Aug 2013 Link to this post

    Thank you for your answer and the provided example. Works fine.
  4. Gowtama
    Gowtama avatar
    2 posts
    Member since:
    Jan 2017

    Posted 26 May 2017 in reply to Dimitar Link to this post

    Hi is there a way to save and load the expanded state of multi level grouping if a group has another say 3 levels of group under it.
  5. Dimitar
    Admin
    Dimitar avatar
    2831 posts

    Posted 29 May 2017 Link to this post

    Hello Gowtama,

    In this case, you need to store the rows and their states and you need to recursively iterate all rows. Here is an example for this:
    Dictionary<object, State> rowStates = new Dictionary<object, State>();
    public RadForm1()
    {
        InitializeComponent();
        radGridView1.GroupDescriptors.Add("Name", ListSortDirection.Ascending);
        radGridView1.GroupDescriptors.Add("Dosage", ListSortDirection.Ascending);
        radGridView1.DataSource = GetTable();
    }
     
    static DataTable GetTable()
    {
     
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
     
        for (int i = 0; i < 5; i++)
        {
     
     
            table.Rows.Add(25, "Indocin", "David", DateTime.Now);
            table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
            table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        }
        return table;
    }
    private void radButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRowInfo nodeToSave in radGridView1.ChildRows)
        {
            SaveExpandedStates(nodeToSave);
        }
     
    }
     
    private void SaveExpandedStates(GridViewRowInfo rowToSave)
    {
        if (rowToSave is GridViewGroupRowInfo)
        {
            var row = rowToSave as GridViewGroupRowInfo;
     
            if (!rowStates.ContainsKey(rowToSave))
            {
                rowStates.Add(rowToSave, new State(row.IsExpanded, rowToSave.IsSelected));
            }
            else
            {
                rowStates[rowToSave] = new State(rowToSave.IsExpanded, rowToSave.IsSelected);
            }
     
            foreach (GridViewRowInfo childNode in rowToSave.ChildRows)
            {
                SaveExpandedStates(childNode);
            }
        }
    }
     
    private void radButton2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRowInfo nodeToSave in radGridView1.ChildRows)
        {
            RestoreExpandedStates(nodeToSave);
        }
    }
     
    private void RestoreExpandedStates(GridViewRowInfo nodeToRestore)
    {
        if (nodeToRestore != null && rowStates.ContainsKey(nodeToRestore))
        {
            nodeToRestore.IsExpanded = rowStates[nodeToRestore].Expanded;
            nodeToRestore.IsSelected = rowStates[nodeToRestore].Selected;
        }
     
        foreach (GridViewRowInfo childNode in nodeToRestore.ChildRows)
        {
            RestoreExpandedStates(childNode);
        }
    }
    struct State
    {
        public bool Expanded { get; set; }
     
        public bool Selected { get; set; }
     
        public State(bool expanded, bool selected) : this()
        {
            this.Expanded = expanded;
            this.Selected = selected;
        }
    }

    I hope this will be useful.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. Gowtama
    Gowtama avatar
    2 posts
    Member since:
    Jan 2017

    Posted 30 May 2017 in reply to Dimitar Link to this post

    Yeah. It's working now. Thank you.
Back to Top