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

GroupsDefaultExpanded after sorting/reorder

1 Answer 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JP
Top achievements
Rank 1
JP asked on 05 Jun 2012, 01:25 PM
Hello,

I want the Grid to be collapsed when it is initialized the first time. So I set 
MasterTableView.GroupsDefaultExpanded = false;
But if the user expands one group and sorts or reorders a column, all groups are collapsed, even the node the user just expanded.
Do I have to keep track myself which nodes were expanded by the user?

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Jun 2012, 01:48 PM
Hi,

Please take a look into the following code snippet i tried to retain the grouping even after sorting or reordering.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" ShowFooter="true" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource"
AllowSorting="true" ShowGroupPanel="True" OnGroupsChanging="RadGrid1_GroupsChanging"
OnItemCommand="RadGrid1_ItemCommand" AutoGenerateEditColumn="true">
<ClientSettings AllowDragToGroup="True">
</ClientSettings>
<MasterTableView GroupsDefaultExpanded="false" EditMode="InPlace">
    <GroupByExpressions>
        <telerik:GridGroupByExpression>
            <GroupByFields>
                <telerik:GridGroupByField FieldName="Group" />
            </GroupByFields>
            <SelectFields>
                <telerik:GridGroupByField FieldName="Group" />
            </SelectFields>
        </telerik:GridGroupByExpression>
    </GroupByExpressions>
</MasterTableView>
</telerik:RadGrid>

C#:
public Hashtable Groups
{
    get
    {
        if (ViewState["Groups"] == null)
        {
            Hashtable res = new Hashtable();
            ViewState["Groups"] = res;
            return res;
        }
        return (Hashtable)ViewState["Groups"];
    }
    set
    {
        ViewState["Groups"] = value;
    }
}
 
protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.DataBound += new EventHandler(RadGrid1_DataBound);
}
 
void RadGrid1_DataBound(object sender, EventArgs e)
{
    foreach (GridGroupHeaderItem item in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        if (Groups.ContainsKey(item.DataCell.Text))
        {
            item.Expanded = (bool)Groups[item.DataCell.Text];
        }
    }
}
 
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
 
    DataTable tbl = new DataTable();
    DataColumn col = new DataColumn("ID");
    col.DataType = typeof(int);
    tbl.Columns.Add(col);
    col = new DataColumn("Name");
    col.DataType = typeof(string);
    tbl.Columns.Add(col);
    col = new DataColumn("Group");
    col.DataType = typeof(string);
    tbl.Columns.Add(col);
 
    int size = 15;
    int maxLen = size.ToString().Length;
    for (int i = 1; i <= size; i++)
    {
        tbl.Rows.Add(new object[] { i, "Name" + i.ToString("D" + maxLen), "Group" + i % 5 });
    }
    RadGrid1.DataSource = tbl;
}
protected void RadGrid1_GroupsChanging(object source, GridGroupsChangingEventArgs e)
{
    Groups.Clear();
    foreach (GridGroupHeaderItem item in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
    {
        Groups[item.DataCell.Text] = item.Expanded;
    }
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.ExpandCollapseCommandName)
    {
        Groups[((GridGroupHeaderItem)e.Item).DataCell.Text] = !e.Item.Expanded;
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
JP
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or