简体   繁体   中英

How to GroupBy on Telerik RadGrid from code


I've seen you can do on Telerik's RadGrid like this:上执行

<GroupByExpressions>
   <telerik:GridGroupByExpression>
      <SelectFields>
         <telerik:GridGroupByField FieldAlias="Received" FieldName="Received" FormatString="{0:D}"
          HeaderValueSeparator=" from date: "></telerik:GridGroupByField>
      </SelectFields>
      <GroupByFields>
         <telerik:GridGroupByField FieldName="Received" SortOrder="Descending"></telerik:GridGroupByField>
      </GroupByFields>
   </telerik:GridGroupByExpression>
</GroupByExpressions>

But I'm trying to group employees .对员工进行分组。 I've tryed this (employeelist have 27 rows):

Telerik.Web.UI.GridGroupByExpression groupby = new Telerik.Web.UI.GridGroupByExpression();

groupby.SelectFields.FindByName("EmployeeSoc");
groupby.GroupByFields.FindByName("EmployeeSoc");
gridEmployees.MasterTableView.GroupByExpressions.Add(groupby);

gridEmployees.PageSize = employeelist.Count();
gridEmployees.DataSource = employeelist;

grdDatos.Rebind();

Nothing goes wrong during the debug, but grid says 'No records to display.'.

You are trying to do a programmatic definition of a group by expression in RadGrid.

There are three things I see that might be incorrect with the code you have posted. Try to take a look at each and reach a possible solution or move you along past your current error:

  1. You have not defined an Aggregate Function. Each grouping needs to have an aggregate function (Sum, Avg, Max, Min, etc)

  2. Your list is not wired into your RadGrid correctly. When I see the error

No records to display

this is means no rows from the List are displayed regardless of the groupings. Remove the grouping code and get the employees displaying correctly without groupings in your RadGrid.

  1. Your SelectField "EmployeeSoc" is the same as your GroupByField . This is okay to do - but typically one likes to group one column against the other. (Price per Product, or Average Age per category, etc)

Please post your data model in your question and your grouping goal from a requirements perspective with EmployeeList and we can try to help you further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM