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

Save/Load scrollbar position and expanded state of groups

5 Answers 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Johannes
Top achievements
Rank 1
Johannes asked on 22 Aug 2013, 12:50 PM
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.

5 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 27 Aug 2013, 12:09 PM
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 >>
0
Johannes
Top achievements
Rank 1
answered on 27 Aug 2013, 02:20 PM
Thank you for your answer and the provided example. Works fine.
0
Gowtama
Top achievements
Rank 1
answered on 26 May 2017, 04:16 PM
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.
0
Dimitar
Telerik team
answered on 29 May 2017, 11:42 AM
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.
0
Gowtama
Top achievements
Rank 1
answered on 30 May 2017, 07:47 PM
Yeah. It's working now. Thank you.
Tags
GridView
Asked by
Johannes
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Johannes
Top achievements
Rank 1
Gowtama
Top achievements
Rank 1
Share this question
or