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

Autogenerate numbers in RadGrid column based on group in GroupByExpressions RadGrid

2 Answers 58 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Priyanka
Top achievements
Rank 1
Priyanka asked on 17 Sep 2015, 06:01 AM

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. 
Example:
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.
Example:
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 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 22 Sep 2015, 10:25 AM
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">
    <MasterTableView>
        <Columns>
            <telerik:GridTemplateColumn UniqueName="InvoiceLineNo" HeaderText="Invoice Line No">
                <ItemTemplate>
                    <asp:Label Text="" runat="server" ID="Label1"/>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn UniqueName="InvoiceNo" HeaderText="Invoice No">
                <ItemTemplate>
                    <asp:Label Text="" runat="server" ID="Label2"/>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="BoolValue" />
                </GroupByFields>
                <SelectFields>
                    <telerik:GridGroupByField FieldName="BoolValue" />
                </SelectFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
    </MasterTableView>
</telerik:RadGrid>

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)
    {
        groupInlineNo++;
        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.


Regards,
Konstantin Dikov
Telerik
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
0
Priyanka
Top achievements
Rank 1
answered on 23 Sep 2015, 08:54 AM
Thank you for the help Dikov.
Tags
General Discussions
Asked by
Priyanka
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Priyanka
Top achievements
Rank 1
Share this question
or