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

Programmatically add group by expression TO THE END OF COLLECTION

6 Answers 799 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 14 Oct 2011, 10:12 AM

Hi, I’m a bit stuck. I am using the below to add a custom group by expression to a rad grid. I want the user to be able to group by any of the other fields in the grid, but always have my custom sort last (I have used e.canceled to stop them deleting it and was going to look at removing the group by box from the group by bar as well).

I have tried changing the index of the item, removing the item, then inserting it, but even this doesn’t seem to work. I have tried adding it back intot he collection on prerender, but even this adds it before the new expression.

I’m sure it’s simple, but I can’t see how to do it. All i want is for the user to be able to group as they want, and then add this last grouping after any other group by expressions that they might have added.

 

 

 

Dim thestring As String
thestring = "nominal_id [Nominal ID], first_name [First name],middle_name [Middle name],last_name [Surname],nicknames [Nicknames],dob [DOB],place_of_birth [Place Of Birth],miss_count [Files], photograph [photo] Group By nominal_id"
Dim expression1 As GridGroupByExpression = GridGroupByExpression.Parse(thestring)

 

 

Me.RadGrid1.MasterTableView.GroupByExpressions.Add(expression1)

 

 

 

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Oct 2011, 03:13 PM
Hello,

Please look in this

Thanks,
Jayesh Goyani
0
James
Top achievements
Rank 1
answered on 17 Oct 2011, 09:13 AM

I’m sorry, I obviously didn’t make my problem clear enough.

 I have used that example, and others to get to the point that I am at.

 What I have is a radgrid that has a custom group by expression applied to it when the page first loads. I ALWAYS want this grouping applied. I have stopped the user removing this grouping (using e.canceled=true ongroupchanging having detected if it is an ungroup action, and the group by expression being removed is the one I want to keep).

What I want is for the group by expression that I have added to ALWAYS be the last group by statement for example. The page loads. My group by expression is added.

The grid is grouped by Nominal ID (the expression I want to always keep and be last)

The user groups by data of birth. The grid is now grouped by Nominal ID, Date of Birth. I want it to force the group by order to be Date of Birth, Nominal ID, so that nominal ID is always the last grouping applied. I also want to make sure it is the last even if the user tried to change the group by order. i am happy for them to have any grouping and any order that they want AS LONG AS NOMINAL_ID is grouped LAST

 

0
Radoslav
Telerik team
answered on 20 Oct 2011, 09:59 AM
Hello James,

To achieve the desired functionality you could try using the following code snippet:
void RadGrid1_PreRender(object sender, EventArgs e)
    {
        for (int i = 0; i < RadGrid1.MasterTableView.GroupByExpressions.Count - 1; i++)
        {
            if (RadGrid1.MasterTableView.GroupByExpressions[i].GroupByFields[0].FieldName == "ID")
            {
                RadGrid1.MasterTableView.GroupByExpressions.RemoveAt(i);
            }
        }
 
        GridGroupByField field = new GridGroupByField();
        field.FieldName = "NominalID";
        field.HeaderText = "NominalID";
        GridGroupByExpression ex = new GridGroupByExpression();
        ex.GroupByFields.Add(field);
        ex.SelectFields.Add(field);
        RadGrid1.MasterTableView.GroupByExpressions.Add(ex);
 
        RadGrid1.Rebind();
    }

Additionally I am sending you a simple example. Please check it out and let me know if it helps you.

Looking forward for your reply.

Regards,
Radoslav
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
0
James
Top achievements
Rank 1
answered on 20 Oct 2011, 02:39 PM
Hi Radoslav, the only problem wiht this is that the groups no longer expand/retract - this is also the case in yoru example.

i had already trie something very simialr, but missed out the radgrid.rebind (stupid me). it seems to be the rebind call that stope the group expand/retract functionality working.
0
Radoslav
Telerik team
answered on 24 Oct 2011, 08:24 AM
Hello James,

Please try using the following code snippet and let me know if the issue still persists:
bool shouldRebind = true;
    void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            shouldRebind = false;
        }
    }
 
    void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (shouldRebind)
        {
            for (int i = 0; i < RadGrid1.MasterTableView.GroupByExpressions.Count; i++)
            {
                if (RadGrid1.MasterTableView.GroupByExpressions[i].GroupByFields[0].FieldName == "ID")
                {
                    RadGrid1.MasterTableView.GroupByExpressions.RemoveAt(i);
                }
            }
 
            GridGroupByField field = new GridGroupByField();
            field.FieldName = "ID";
            field.HeaderText = "ID";
            GridGroupByExpression ex = new GridGroupByExpression();
            ex.GroupByFields.Add(field);
            ex.SelectFields.Add(field);
            RadGrid1.MasterTableView.GroupByExpressions.Add(ex);
 
            RadGrid1.Rebind();
        }
    }

Looking forward for your reply.

Greetings,
Radoslav
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
0
James
Top achievements
Rank 1
answered on 25 Oct 2011, 11:30 AM
Hi Radoslav, that's perfect, thanks
Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
James
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or