DataGridView - 列实时汇总 [英] DataGridView - Columns sum in real time
问题描述
大家好,
我有一个问题,我有一个带发票信息的DataGridView。其中一列是服务的价格。
正下面我有一个标签,需要显示此发票的总价值,它需要是显示为用户输入值。
例如
服务|价值
酒店| $ 200.00
总计:$ 200.00
输入下一个条目后......
服务|价值
酒店| $ 200.00
出租车| $ 50.00
总计:$ 250.00
此外,我不知道如何使用活动。那么,如果你能解释我如何宣布一个事件...
我知道我必须使用这个方法(例如 private void dgrid_CellValueChanged(object sender) ,DataGridViewCellEventArgs e){...} ),但实际上我不知道如何在进行单元格更新时调用此方法。
在此之后,当我尝试使用为Pawan提出的解决方案时,我收到以下错误:
对象引用未设置为对象的实例。
在线:
double V = string.IsNullOrEmpty(dgrid.Rows [i] .Cells [Service]。Value.ToString())? 0:Convert.ToDouble(dgrid.Rows [i] .Cells [Service]。Value);
PS:我在Form Load事件中将此列设置为typeof(double),我也使用列格式作为C2(货币)。在这种情况下,输入值后,它会将符号$放在值的前面。此外,当我在其中一列中输入内容时,会自动创建一个新行。
有人可以帮我吗? :confused:
提前致谢! ;)
Hi all,
I have a problem where I have a DataGridView with invoice information. One of the columns is the price of a service.
Right below this I have a label that needs to show the total value of this invoice, and it needs to be shown as the user types the values.
e.g.
Service | Value
Hotel | $200.00
Total: $200.00
After typing the next entry...
Service | Value
Hotel | $200.00
Taxi | $50.00
Total: $250.00
Also, I don't know how to use events. So, if you can explain me how to declare an event...
I know I must use method for this (e.g. private void dgrid_CellValueChanged(object sender, DataGridViewCellEventArgs e){ ... } ), but I don't know how to make this method to be called when there is a cell updating, in fact.
After this, when I try to use the solution proposed for Pawan, I got the following error:
"Object reference not set to an instance of an object."
On the line:
double V = string.IsNullOrEmpty(dgrid.Rows[i].Cells["Service"].Value.ToString()) ? 0 :Convert.ToDouble(dgrid.Rows[i].Cells["Service"].Value);
PS: I set this column as typeof(double) in the Form Load event and I am also using a column format as C2(currency). In this case, after entering the value, it puts the symbol $ in front of the value. Also, when I type something in one of the columns, a new line is automatically created.
Can someone help me? :confused:
Thanks in advance! ;)
推荐答案
200.00
总计:
200.00
Total:
200.00
输入下一个条目后......
服务|价值
酒店|
200.00
After typing the next entry...
Service | Value
Hotel |
200.00
出租车|
200.00
Taxi |
这篇关于DataGridView - 列实时汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!