如何在datagridview中计算时设置条件 [英] How to set condition when calculate in datagridview

查看:121
本文介绍了如何在datagridview中计算时设置条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个问题需要你的帮助。



问题:



我有datagridview有这样的5列



数量价格TotalAmount折扣TotalAmountAfterDiscount



表达式为:



TotalAmount =数量*价格

TotalAmountAfterDiscount = TotalAmount * Discount / 100(问题!!!)



如果Discount = 0%,表达式将出错。所以我想在这里设置条件如果Discount细胞的值等于0我们将有其他表达式TotalAmountAfterDiscount = TotalAmount



请帮我解决这个问题。非常感谢你



@note:我正在使用DataColumnName.Expression来计算和datagridview数据源绑定到由代码创建的临时表,因为我想要计算所有之后的值将所有数据插入数据库

Hello everyone , I have a question need your help .

Question :

I have a datagridview have 5 columns like this

Quantity Prices TotalAmount Discount TotalAmountAfterDiscount

The expression is :

TotalAmount = Quantity * Prices
TotalAmountAfterDiscount = TotalAmount * Discount/100 ( Problem here !!!)

If Discount = 0% the expression will go wrong . So i want set condition here if values of Discount cells equal 0 we will have other expression TotalAmountAfterDiscount = TotalAmount

Please help me solve this problem . Thanks you a lot

@note : I'm using DataColumnName.Expression to calculate and datagridview datasource is bound to temp table which created by code because i want to calculate all values after that insert all of it to database

推荐答案

在我看来,您的折扣后总金额的公式是不正确的。您使用TotalAmount计算的内容*折扣/ 100是折扣金额,而不是折扣后的总金额,即总金额 - 折扣金额,对吧?修复你的公式并修复你的问题。
Looks to me like your formula for the total amount after discount is incorrect. What you're calculating with TotalAmount * Discount/100 is the amount of the discount, not the total amount after discount, which would be total amount - discount amount, right? Fix your formula and you fix your problem.


你确实意识到那个表达式无论如何都不会起作用吗?

You do realize that that expression won't work anyway?
Price = 100
Quantity = 10
TotalAmount = P * Q = 1000
Discount = 25%
TotalAmountAfterDiscount = (P * Q) * D / 100 = 1000 * 25 / 100 = 250



你真正想要的是:


What you actually want is:

Price = 100
Quantity = 10
TotalAmount = P * Q = 1000
Discount = 25%
TotalAmountAfterDiscount = (P * Q) - ((P * Q) * D / 100) = 1000 - (1000 * 25 / 100) = 1000 - 250 = 750





因此,更改您的TotalAmountAfterDiscount计算,0%折扣将正常工作。



So change your TotalAmountAfterDiscount calculation, and 0% discount will work fine.


这篇关于如何在datagridview中计算时设置条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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