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

Show totals of two grid in the second grid.

3 Answers 31 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felipe de Jesús
Top achievements
Rank 1
Felipe de Jesús asked on 06 Sep 2011, 03:30 PM
Dear Telerik Team:

I have two grid that i fill with the results of two different SQL queries, but with the same numbers and types of collumns. It's possible to show the totals of all elements from the two grids but only in the second grid.

For example:

Ingredient Percent Weight
Element 1 10% 600 Kg
Element 2 15% 900 Kg
Element 3 20% 1200 Kg

Ingredient Percent Weight
Element 1 30% 1800 Kg
Element 2 5% 300 Kg
Element 3 8% 480 Kg
Element 4 12% 720 Kg
TOTAL 100% 6000 Kg

Please, can you provide me a solution?

Kind regards.
Felipe de Jesús Meléndez

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 06 Sep 2011, 06:45 PM
Hello,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx.cs" Inherits="TelerikTest.Web.WebForm11" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
            AutoGenerateColumns="false">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <br />
         <telerik:RadGrid ID="RadGrid2" runat="server" OnNeedDataSource="RadGrid2_NeedDataSource"
            AutoGenerateColumns="false">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <br />
        <telerik:RadGrid ID="RadGrid3" runat="server" OnNeedDataSource="RadGrid3_NeedDataSource"
            AutoGenerateColumns="false">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="TotalIDs" UniqueName="TotalIDs" HeaderText="TotalIDs">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace TelerikTest.Web
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                new { ID = 1, Name ="Name1"},
                new { ID = 2, Name = "Name2"},
                new { ID = 3, Name = "Name3"},
                new { ID = 4, Name = "Name4"},
                new { ID = 5, Name = "Name5"}
            };
 
            RadGrid1.DataSource = data;
        }
 
        protected void RadGrid2_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                new { ID = 11, Name ="Name1"},
                new { ID = 22, Name = "Name2"},
                new { ID = 33, Name = "Name3"},
                new { ID = 44, Name = "Name4"},
                new { ID = 55, Name = "Name5"}
            };
 
            RadGrid2.DataSource = data;
        }
        protected void RadGrid3_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid3.DataSource = from o in getCalculatedData()
                                  group o by new { o.Name } into Newo
                                  select new
                                  {
                                      Name = Newo.Key.Name,
                                      TotalIDs = Newo.Sum(s => s.ID)
                                  };
 
        }
        protected List<CalculatedData> getCalculatedData()
        {
            List<CalculatedData> lstCalculatedData = new List<CalculatedData>();
 
            foreach (GridDataItem item in RadGrid1.Items)
            {
                CalculatedData obj = new CalculatedData();
                obj.ID = Convert.ToInt32(item["ID"].Text.ToString());
                obj.Name = item["Name"].Text.ToString();
                lstCalculatedData.Add(obj);
            }
 
            foreach (GridDataItem item in RadGrid2.Items)
            {
                CalculatedData obj = new CalculatedData();
                obj.ID = Convert.ToInt32(item["ID"].Text.ToString());
                obj.Name = item["Name"].Text.ToString();
                lstCalculatedData.Add(obj);
            }
 
            return lstCalculatedData;
        }
    }
 
    public class CalculatedData
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Felipe de Jesús
Top achievements
Rank 1
answered on 07 Sep 2011, 02:36 PM
Hi Jayesh Goyani.

Wow. That was pretty fast. Thank you for your time and your solution. I try to implement something similar to fit the case I need.

Kind regards.
Felipe de Jesús Meléndez Valencia.
0
Felipe de Jesús
Top achievements
Rank 1
answered on 20 Oct 2011, 05:48 PM
Hello again.

Well, finally i use a different approach to obtain the desired result.

Anyway thanks for your time.

Kind regards.
Felipe de Jesús Meléndez.
Tags
Grid
Asked by
Felipe de Jesús
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Felipe de Jesús
Top achievements
Rank 1
Share this question
or