Autogenerate numbers in RadGrid column based on group in GroupByExpressions RadGrid

3 posts, 1 answers
  1. Priyanka
    Priyanka avatar
    34 posts
    Member since:
    Dec 2014

    Posted 17 Sep 2015 Link to this post

    I am having a trouble in implementing below requirement.
    Current RadGrid: Attached (Snapshot 1) is the RadGrid in which I am using GroupByExpressions to display/show data grouped with "Business Unit" column.
    In RadGrid column 2nd(InvoiceLineNo) and 3rd(InvoiceNo), I am auto generating the numbers using Stored Procedure.

    i.e., for "InvoiceLineNo" column, Autogenerated No's are: 01,02,03,04,05,06,07,08.......n 
    for "InvoiceNo" column, Autogenerated No's are: 15100001, 15100002, 15100003........n 
    where, 15 is a "year" and 100001 are "running numbers"

    Requirement is: I want to show the "InvoiceLineNo" column data as Group wise. 
    for 1st "Business Unit" group (i.e., SUNWAY LEISURE SDN BHD (CARNIVAL)), InvoiceLineNo shall be: 01,02,03,04,05,06,07,08
    for 2nd "Business Unit" group (i.e., SUNWAY MALL PARKING SDN BHD), InvoiceLineNo shall be: 01,02,03,04,05,06,07,08

    Similarly, I want to show the "InvoiceNo" column data as Group wise.
    for 1st "Business Unit" group (i.e., SUNWAY LEISURE SDN BHD (CARNIVAL)), InvoiceNo shall be: 15100001,15100001,15100001,15100001,15100001,15100001,15100001,15100001
    for 2nd "Business Unit" group (i.e., SUNWAY MALL PARKING SDN BHD), InvoiceNo shall be: 15100002,15100002,15100002,15100002,15100002,15100002,15100002,15100002

    "InvoiceNo" column data will always be unique for different "Business Unit". 

    I want output to be like attached snapshot​ 2.

    I can autogenerate the numbers serial wise but I am not getting how to autogenerate the 2 column values based on Group and show them like that.
    Please help me to achieve it. Please do reply.
    Thanks in advance.

  2. Answer
    Konstantin Dikov
    Konstantin Dikov avatar
    2119 posts

    Posted 22 Sep 2015 Link to this post

    Hi Priyanka,

    There is no built-in way that will allow you to easily achieve the desired functionality, especially if there will be more than one group levels in your RadGrid.

    Nevertheless, for simple scenario with one group, you can use the following approach within the OnPreRender event of the grid:
    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender">
                <telerik:GridTemplateColumn UniqueName="InvoiceLineNo" HeaderText="Invoice Line No">
                        <asp:Label Text="" runat="server" ID="Label1"/>
                <telerik:GridTemplateColumn UniqueName="InvoiceNo" HeaderText="Invoice No">
                        <asp:Label Text="" runat="server" ID="Label2"/>
                        <telerik:GridGroupByField FieldName="BoolValue" />
                        <telerik:GridGroupByField FieldName="BoolValue" />

    And the code-behind:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
        GridItem[] groupHeaderItems = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader);
        int groupInlineNo = 15100000;
        foreach (GridGroupHeaderItem groupHeader in groupHeaderItems)
            int inlineVoiceNo = 1;
            GridItem[] items = groupHeader.GetChildItems();
            for (int i = 0; i < items.Length; i++)
                if (items[i] is GridDataItem)
                    ((items[i] as GridDataItem)["InvoiceLineNo"].FindControl("Label1") as Label).Text = (inlineVoiceNo++).ToString();
                    ((items[i] as GridDataItem)["InvoiceNo"].FindControl("Label2") as Label).Text = groupInlineNo.ToString();

    For more group levels you will have to modify the logic, so it could traverse the inner GridGroupHeaderItems and its child items.

    Hope this helps.

    Konstantin Dikov
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Priyanka
    Priyanka avatar
    34 posts
    Member since:
    Dec 2014

    Posted 23 Sep 2015 in reply to Konstantin Dikov Link to this post

    Thank you for the help Dikov.
Back to Top