如何在datgridview中对重复的行求和并在另一个datagridview中显示 [英] How to sum repeated rows in datgridview and show in another datagridview

查看:80
本文介绍了如何在datgridview中对重复的行求和并在另一个datagridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个datagridviews(第一个名为Dgv_Invoice,第二个名为dgv_costCenter)

第二个datagridview(dgv_costCenter)中的数据是

i have two datagridviews(first one called Dgv_Invoice,and 2nd called dgv_costCenter)
the data in the second datagridview(dgv_costCenter) was

item name	price	quantity	value	customerid
keyboard	20	10		 200        1
mouse	        10	10	         100        2  
monitor	        200     2                400        3
keyboard	20      5                100        4
mouse	        10      5                50         5 
keyboard	20      7                140        6
mouse	        10      7                70         7

我想做的是在第一个datagridview(Dgv_Invoice)中显示项目

what i want to do is to show items in the first datagridview(Dgv_Invoice) like that

item name	price	quantity	value	
keyboard	20	22		 440       
mouse	        10	22	         220          
monitor	        200     2                400





我尝试了什么:



i在我的数据库中创建表名为(项目_transactions)

和解决这个问题我做了一个查询来保存来自dgv_costCenter的数据

带存储过程然后我调用数据显示在Dgv_Invoice里面按itemname分组

按项目名称,价格,数量从items_transactions中选择项目名称,价格,总和(数量)作为总数量。问题是我想在我的表单中没有保存(dgv_costCenter)进入数据库并再次调用数据以显示内部(Dgv_Invoice)查询..任何人都可以帮助我。



What I have tried:

i have create table inside my database called (items_transactions)
and the to solve this problem i made a query to save data from dgv_costCenter
with stored Procedure and then i call the data to show inside Dgv_Invoice grouped by itemname
select itemname,price,sum(quantity)as totalquantity from items_transactions group by itemname,price,quantity . the problem is that i want to do that inside my form without save (dgv_costCenter) into database and call data again to show inside(Dgv_Invoice) with query .. can any one help me.

推荐答案

也许这篇CodeProject文章将帮助您入门: OutlookGrid :以Outlook样式分组和排列项目 [ ^ ]



您还可以将数据复制到临时列表<> 然后使用 Linq 对数据进行分组和求和,例如: c# - Linq:GroupBy,Sum和Count - Stack Overflow [ ^ ]

列表<>然后可以用作DataGridView的 DataSource ,请参见此处的示例: c# - 使用列表作为DataGridView的数据源 - Stack Overflow [ ^ ]
Maybe this CodeProject article will get you started: OutlookGrid: grouping and arranging items in Outlook style[^]

You can also copy the data into a temporary List<> and then use Linq to group and sum the data, example here: c# - Linq: GroupBy, Sum and Count - Stack Overflow[^]
The List<> can then be used as a DataSource for your DataGridView, see example here: c# - Using a list as a data source for DataGridView - Stack Overflow[^]


我不太确定你是不是'再谈WinForm,WPF或ASP.NET / MVC ......前两个答案是数据绑定。然后你不必担心控制和如何访问数据,而是直接在数据上独立于控件。



*对于Winforms,这里是从哪里开始: Windows窗体数据绑定| Microsoft Docs [ ^ ]

*对于WPF:数据绑定(WPF)| Microsoft Docs [ ^ ]
I'm not quite sure if you're talking WinForm, WPF, or ASP.NET/MVC... The answer for the first two is Data Binding. Then you don't have to worry about the control and how to access the data but instead work directly on the data independent of the control.

* For Winforms, here is where to get started: Windows Forms Data Binding | Microsoft Docs[^]
* For WPF: Data Binding (WPF) | Microsoft Docs[^]


以另一种方式回答你的问题:

In answer to your question to do it another way:
for (int c = 0; c < dgv_CostCenter.Rows.Count - 1; c++)
{
    int sum = 0;
    var costname = dgv_CostCenter.Rows[c].Cells[0].Value.ToString();

    for (int i = 0; i < Dgv_Invoice.Rows.Count - 1; i++)
    {
        var invoicename = Dgv_Invoice.Rows[i].Cells[0].Value.ToString();

        if (costname == invoicename)
        {
            int invoiceValue = Convert.ToInt32(Dgv_Invoice.Rows[i].Cells[2].Value);
            sum += invoiceValue;
            dgv_CostCenter.Rows[c].Cells[2].Value = sum;
        }
    }
}


这篇关于如何在datgridview中对重复的行求和并在另一个datagridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆