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

Grouping grid dynamically

1 Answer 271 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rafael
Top achievements
Rank 1
Rafael asked on 02 Oct 2013, 02:20 PM
Hi everyone,

How Can I create my radgrid subdivided by groups and subgroups with structure generated dynamically?
For example, considering the following scenario: I have one item list that represent this structure of groups
and subgroups and I need to do bind it in the radgrid.

Group1
|________Item1.1
|________SubGroup1.1
|          |________Item1.1.1
|          |________SubGroup1.1.1
|________SubGroup1.2
|________SubGroup1.3
           |________Item1.3.1
Group2
|________Subgroup2.1

I am using GridGroupByExpression in codebehind to simulate the grouping in the radgrid, but I could only display the first level of grouping
Group1
|________Item1.1
|________SubGroup1.1
|________SubGroup1.2
|________SubGroup1.3
Group2
|________Subgroup2.1

Below is my radgrid

<telerik:RadGrid ID="radGrid" Width="100%" Skin="Silk" AllowSorting="True" ShowFooter="true"
AllowPaging="True" runat="server" AutoGenerateColumns="False" ShowStatusBar="True"
AllowMultiRowSelection="true"
EnableLinqExpressions="False" CellSpacing="0" GridLines="None" OnItemCommand="radGrid_ItemCommand"
        OnItemDataBound="radGrid_ItemDataBound" >
<ClientSettings AllowDragToGroup="true" AllowColumnsReorder="true" AllowGroupExpandCollapse="true"
    ReorderColumnsOnClient="true" 
ColumnsReorderMethod="Reorder" EnableRowHoverStyle="true">
    <Selecting AllowRowSelect="true"/>
    <Animation AllowColumnReorderAnimation="true" AllowColumnRevertAnimation="true">
    </Animation
</ClientSettings>
        <MasterTableView DataSourceID="odsGrupoProduto" DataKeyNames="IdItem,IdGroup"
                 
NoMasterRecordsText="Nenhum registro encontrado.">
        <Columns>
                 <telerik:GridBoundColumn DataField="Description" HeaderText="Description"
     SortExpression="Description" UniqueName="Description">
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                 </telerik:GridBoundColumn>
 <telerik:GridTemplateColumn Reorderable="false" Resizable="false">
                        <ItemTemplate>
                        <asp:ImageButton ID="ibEditSubGroup" runat="server"
ToolTip="Edit SubGroup."
                                        CommandArgument='<%# Eval("IdGroup")%>'
       CommandName="Edit"
       ImageUrl="~/imagems/edit.png" />
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                 </telerik:GridTemplateColumn>
                 <telerik:GridTemplateColumn Reorderable="false" Resizable="false">
                        <ItemTemplate>
                                 <asp:ImageButton ID="ibRemove" runat="server"
ToolTip="Remove item."
                                        CommandArgument='<%# Eval("IdItem")%>'
       CommandName="Remove"
                                                       ImageUrl="~/imagems/delete.png" />
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                 </telerik:GridTemplateColumn>
       </Columns>
 </MasterTableView>
</telerik:RadGrid>
<asp:ObjectDataSource ID="ods" runat="server" SelectMethod="ListItems"
TypeName="ControllerGroup">
</asp:ObjectDataSource>

I set GridGroupByExpression in the method that load my radgrid:

private void LoadGrid()
{
       ods.SelectParameters.Clear();
       radGrid.DataSourceID = "ods";
       ods.DataBind();
 
       GridGroupByExpression expression1 = GridGroupByExpression.Parse("Type Group By Type");
       this.radGrid.MasterTableView.GroupByExpressions.Add(expression1);
       radGrid.Rebind();
}

Below is my class of item of list

public class Item
{
     public int? IdGroup;
     public string Description;
     public string Type;
     public int? IdGroupParent;
     public int? IdItem;
}
From my class, the field Type can assume the following values: "Group","SubGroup" and "Item".

Please, give me ideas about how can I resolve this issue!

Thanks!

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 07 Oct 2013, 01:13 PM
Hi Rafael,

Please note that using DataBind() is not recommended. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:
Declarative DataSource
Advanced Data Binding

If you want to fire a Group command, you can use the following approach:
http://www.telerik.com/help/aspnet-ajax/grid-gridtableview-firecommand.html

In case you want to add the GroupByExpressions programmatically, you can refer to this sample:
http://demos.telerik.com/aspnet-ajax/grid/examples/groupby/expressions/defaultcs.aspx

Hope this helps.

Regards,
Eyup
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Rafael
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or