移动到新行C#时DataGridView计算列更新 [英] DataGridView Calculated Column update when move to new row C#

查看:49
本文介绍了移动到新行C#时DataGridView计算列更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我使用Visual Studio 2010和Sql server Express Edition 2005作为后端数据库。我有一个带有DataGridView的表单,其中包含以下列:



OrderID ProductID UnitPrice Quantity Total



DataGridView绑定到数据集表,总计是计算列,我使用以下代码添加:



Hi to All,

I am using Visual Studio 2010 with Sql server Express Edition 2005 as backend DB. I've a form with DataGridView with following columns:

OrderID ProductID UnitPrice Quantity Total

DataGridView is bind to Dataset Table, "Total" is a Calculated column which I've added using following code:

column = new DataColumn();
column.DataType = Type.GetType("System.Double");
column.ColumnName = "Total";
ds.Tables[0].Columns.Add(column);
ds.Tables[0].Columns["Total"].Expression = "UnitPrice * Quantity";





代码工作正常,它通过将UnitPrice乘以Quantity来计算总计。问题是当我移动到新行时进行此计算。我需要的是当UnitPrice或Quantity有任何变化时计算Total。



有可能吗?任何想法/建议将受到高度赞赏。



问候



Ahmed



Code works fine, it calculate the Total by multiplying UnitPrice to Quantity. The problem is this calculation is made when I move to new row. What I need is to it calculate Total when there is any change in UnitPrice or Quantity.

Is it possible? Any ideas/suggestions will be highly appreciated.

Regards

Ahmed

推荐答案

使用 DataGrigViewCell的方法 [ ^ ],例如 OnLeave ,并强制数据表重新计算总计使用计算方法 [ ^ ]。
Use one of DataGrigViewCell's method[^], for example OnLeave, and force datatable to re-calculate Total using Compute method[^].


这篇关于移动到新行C#时DataGridView计算列更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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