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

Editor won't open when GroupsDefaultExpanded is false

3 Answers 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeanne
Top achievements
Rank 1
Jeanne asked on 24 Oct 2012, 04:38 PM
I have a radGrid that is grouped by a certain text field. When GroupsDefaultExpanded is true (or not specified) clicking the Edit button open the popup form template correctly. But when I set GroupsDefaultExpanded to false, the Edit button no longer opens the editor popup. Clicking the Edit buttons posts back to the server, the ItemCommand event is executed with CommandName equal to "Edit", but then the group collapses itself, and the editor never appears! Clicking the AddItem link does open the editor successfully. If I change nothing more than the value of GroupsDefaultExpanded this behavior appears and disappears, so it is obviously related.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Oct 2012, 06:34 AM
Hi,

Please check the following code snippet I tried to show popup edit on setting GroupsDefaultExpanded to false.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowSorting="true" ShowGroupPanel="True" OnGroupsChanging="RadGrid1_GroupsChanging" OnItemCommand="RadGrid1_ItemCommand" AutoGenerateEditColumn="true" OnPreRender="RadGrid1_PreRender">
    <ClientSettings AllowDragToGroup="True">
    </ClientSettings>
    <MasterTableView GroupsDefaultExpanded="false" GroupLoadMode="Client" EditMode="PopUp">
        <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;
    }
    if (e.CommandName == RadGrid.EditCommandName && RadGrid1.MasterTableView.GroupByExpressions.Count > 0)
        group = e.Item.GroupIndex.Substring(0, e.Item.GroupIndex.LastIndexOf("_"));
}
protected void Button1_Click(object sender, EventArgs e)
{
    RadGrid1.Rebind();
}
string group = string.Empty;
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(group))
    {
        GridItem[] items = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader);
        foreach (GridGroupHeaderItem item in items)
            if (item.GroupIndex == group)
                item.Expanded = true;
    }
}

Thanks,
Princy.
0
Jeanne
Top achievements
Rank 1
answered on 25 Oct 2012, 02:11 PM
Did you try it without all the additional code? Is it broken like I described? You also don't have a form template, which might make a difference. Either way, if it takes all this additional code to make it work, it's broken. Specifying GroupsDefaultExpanded="false" should simply do what it says, without requiring additional code to make editing work.

Regardless, I already found the source code you posted and implemented it (with GroupsDefaultExpanded not specfied, i.e. true) and it almost works fine. There is a bug in the implementation. I would have posted a reply to mention it, but the source code library doesn't allow replies. The problem is that if the group header item contains an aggregate value that can change as you work, such as count or sum, the the text of the group header will change when the records in the group change, and it will no longer match what you stored. To get around the problem you have to parse the constant part out of the group header and use only that. You should add a note to the source code posting to that effect. Also, for some reason I did not need the PreRender event.  I left GroupsDefaultExpanded to it's default of true, and initially collapsed all but the first group in the DataBound event like this:
Protected Sub rgNews_DataBound(sender As Object, e As System.EventArgs) Handles rgNews.DataBound
    For Each oItem As GridGroupHeaderItem In rgNews.MasterTableView.GetItems(GridItemType.GroupHeader)
        Dim sGroupKey As String = ParseGroupKey(oItem.DataCell.Text)
        If Not Page.IsPostBack Then
            Groups.Add(sGroupKey, oItem.GroupIndex = "0")
        End If
        If Groups.ContainsKey(sGroupKey) Then
            oItem.Expanded = CBool(Groups(sGroupKey))
        End If
    Next
End Sub

I realize that the GroupIndex would look different if I had multiple-level grouping, but I don't, 

Regardless, there is still the original issue that specifying GroupsDefaultExpanded="false" broke editing. One shouldn't have to add all this code just to make it work like it should.
0
Eyup
Telerik team
answered on 30 Oct 2012, 01:25 PM
Hi Jeanne,

This behavior is due to the fact that the grid makes a PostBack to the server when opening its Edit form and since the GroupsDefaultExpanded property is set to false, all of the groups are being collapsed.

I will forward this issue to our developers who will analyze the code library and the described problem. I will reply to you when they are ready with the result.

Thank you for your valuable feedback.

All the best,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Jeanne
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jeanne
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or