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


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
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

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.
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
