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

RadGrid Edit Groups expand problem

1 Answer 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tan
Top achievements
Rank 1
Tan asked on 18 Apr 2012, 08:24 AM
To:



When default expand is false, while click edit button all will not expand, how to make the group expand while editing?

Thanks


1 Answer, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 22 May 2012, 12:37 PM
Hi Tan,

Please try the sample code snippet i tried to keep the Radgrid Group in Expand mode while editing.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" 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;
    }
}

Please refer this code library for more information.

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