GridTableView aggregate program

1 Answer 82 Views
Grid
AFS331
Top achievements
Rank 1
AFS331 asked on 12 Apr 2022, 10:17 AM

I have a main data table and a sub data table.  But in the sub data table, even if there is no data, the total number appears.

This is my code in the main table for CustomAggregate . Can the total part of my sub table be handled here?

 private void Grid_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
        {

            RadGrid grid = PlaceHolder1.FindControl("grid") as RadGrid;
            string colName = e.Column.UniqueName;
     

            if (e.Item is GridFooterItem)
            {
               
                if (colName  == "ProjectID")
                {
                //    decimal counter = 0;
                
                    e.Result =  "Total hours of timesheet";// counter;
                }

                if (colName != "ProjectAlias" || colName != "ProjectID")
                {
           

                    decimal value = ClsTimeSheet.GetTimeSheetDateTotalHour(LbEmplNo.Text, LbPeriodCode.Text, colName);
                    decimal value2 = ClsTimeSheet.GetTimeSheetCPDateTotalHour(LbEmplNo.Text, LbPeriodCode.Text, colName);
                    e.Result =  (value +  value2).ToString();
                }
            }
        }

 

Please Help. Terry

Attila Antal
Telerik team
commented on 15 Apr 2022, 07:45 AM

Hi Terry,

RadGrid will not display numbers randomly. My guess is that number 3 in the footer comes from the logic you have. Unfortunately, the information you provided is insufficient for us to share our feedback. Please share the complete Grid definition you have, and the C# code related to it. You can also show us an example of what the data structure looks like, and what type of values it contains so we can set up a database table with dummy data. Then we will be able to try and replicate the issue locally.

I look forward to receiving the additional information.

AFS331
Top achievements
Rank 1
commented on 15 Apr 2022, 09:18 AM

Here is my code , thank you

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 15 Apr 2022, 10:22 AM

Hello Terry,

Thanks for sharing the details.

I have looked at the source code and I have isolated the problem from that I can tell the Grid does not make false calculations. The "value" and "value2" variables return the number that you see in the footer, that "3".

The sum of the two values returns 3 and that is what the logic is told to display:

decimal value = ClsTimeSheet.GetTimeSheetDateTotalHour(LbEmplNo.Text, LbPeriodCode.Text, colName);
decimal value2 = ClsTimeSheet.GetTimeSheetCPDateTotalHour(LbEmplno.Text, LbPeriodCode.Text, colName);

 

Here is a runnable example that will do the same exact thing:

Create a new Web Forms page and add the following Placeholder to it

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

 

Add the following C# code to the aspx.cs file

protected void Page_Init(object sender, EventArgs e)
{
    RadGrid grid = new RadGrid();
    grid.ID = "grid";
    grid.Width = Unit.Pixel(300);
    grid.ShowFooter = true;

    grid.NeedDataSource += Grid_NeedDataSource;
    grid.CustomAggregate += Grid_CustomAggregate;

    grid.AutoGenerateColumns = false;

    var boundColumn = new GridNumericColumn();
    boundColumn.DataField = "ProjectID";
    boundColumn.HeaderText = "Header Text";
    boundColumn.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
    boundColumn.Aggregate = GridAggregateFunction.Custom;
    boundColumn.FooterAggregateFormatString = "{0}";

    grid.MasterTableView.Columns.Add(boundColumn);

    PlaceHolder1.Controls.Add(grid);
}

private void Grid_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
{
    string colName = e.Column.UniqueName;

    if (e.Item is GridFooterItem)
    {
        string msg = "- ";

        if (colName == "ProjectID")
        {
            //    decimal counter = 0;

            e.Result = "Total hours of timesheet";// counter;
        }

        if (colName != "ProjectAlias" || colName != "ProjectID")
        {


            // The following two lines are the problem. Value + Value2 return a number that you do not expect.
            //decimal value = ClsTimeSheet.GetTimeSheetDateTotalHour(LbEmplNo.Text, LbPeriodCode.Text, colName);
            //decimal value2 = ClsTimeSheet.GetTimeSheetCPDateTotalHour(LbEmplno.Text, LbPeriodCode.Text, colName);

            // Hardcoded values to mimic the values returned by the lines above
            decimal value = 1;
            decimal value2 = 2;

            e.Result = (value + value2).ToString();
        }
    }
}

private void Grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    var grid = (RadGrid)sender;
    grid.DataSource = Enumerable.Range(0, 0).Select(x => new
    {
        ProjectID = x,
    }).ToList();
}

 

Result

 

To solve the issue, you will need to review the logic and be sure that the variables return the exact value you are expecting.

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

AFS331
Top achievements
Rank 1
commented on 18 Apr 2022, 12:36 PM

Hello Attila,

Although the code you gave me doesn't solve my problem, you gave me the idea. Thank you very much.

Terry

Just modifying in CustomAggregate can solve my problem. Below is my code

  private void Grid_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
        {

            RadGrid grid = PlaceHolder1.FindControl("grid") as RadGrid;
            string colName = e.Column.UniqueName;


            if (e.Item is GridFooterItem)
            {

                if (colName == "ProjectID")
                {

                    e.Result = "Total hours of timesheet";// counter;

                }

                if (colName != "ProjectAlias" || colName != "ProjectID")
                {
                    string GridType = e.Item.OwnerTableView.Name;

                    decimal value;
                    decimal value2;
                    switch (GridType)
                    {
                        case "MainGrid":
                            value = ClsTimeSheet.GetTimeSheetDateTotalHour(LbEmplNo.Text, LbPeriodCode.Text, colName);
                            value2 = ClsTimeSheet.GetTimeSheetCPDateTotalHour(LbEmplNo.Text, LbPeriodCode.Text, colName);
                            e.Result = (value + value2).ToString();
                            break;

                        case "DetailGrid":
                            string key;

                            GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;

                            key = parentItem["ProjectID"].Text;

                            value2 = ClsTimeSheet.GetTimeSheetCPDateProjectHour(LbEmplNo.Text, LbPeriodCode.Text, colName, key);
                            e.Result = (value2).ToString();

                            break;

                    }



                }
            }

        }


Tags
Grid
Asked by
AFS331
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or