移动到新行C#时DataGridView计算列更新 [英] DataGridView Calculated Column update when move to new row C#
问题描述
大家好,
我使用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 exampleOnLeave
, and force datatable to re-calculateTotal
using Compute method[^].
这篇关于移动到新行C#时DataGridView计算列更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!