Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datagridview值的总和修改它并将其添加到其他总和_C#_Winforms_Datagridview - Fatal编程技术网

C# datagridview值的总和修改它并将其添加到其他总和

C# datagridview值的总和修改它并将其添加到其他总和,c#,winforms,datagridview,C#,Winforms,Datagridview,我有这个销售点系统。我已经这样做了好几天了,我似乎真的不能让它工作。我有这个折扣复选框,当你点击 它应该这样做: 检查所有标有“D”的产品,将其总价相加,并将其列为totaldiscountablesales.Text 显示折扣中的总折扣价格。文本 小计仍应为所有价格的总和,无论是否包含D Totaldue应为小计-折扣总额 我不知道我的代码出了什么问题,这是我目前所能做到的 private void cbDiscount2\u CheckedChanged(对象发送方,事件参数e) { 双和

我有这个销售点系统。我已经这样做了好几天了,我似乎真的不能让它工作。我有这个折扣复选框,当你点击 它应该这样做:

  • 检查所有标有“D”的产品,将其总价相加,并将其列为totaldiscountablesales.Text

  • 显示折扣中的总折扣价格。文本

  • 小计仍应为所有价格的总和,无论是否包含D
  • Totaldue应为小计-折扣总额
  • 我不知道我的代码出了什么问题,这是我目前所能做到的

    private void cbDiscount2\u CheckedChanged(对象发送方,事件参数e)
    {
    双和=0;
    双Dsum=0;
    双进位,不含OUDD=0;
    双Dsumless20;
    双Dsumless20plussum;
    if(cbDiscount2.Checked==true)
    {
    对于(int i=0;i
    您可能已经让这个问题变得比需要的更复杂了。Try是这样的,您只跟踪您关心的两个总数:购物车总数和折扣商品:

    decimal cartTotal = 0;
    decimal discountTotal = 0;
    for (int i = 0; i < dgvPOScart.Rows.Count; ++i) {
      cartTotal += Convert.ToDecimal(dgvPOScart.Rows[i].Cells[5].Value);
      if (cbDiscount2.Checked && dgvPOScart.Rows[i].Cells[6].Value.ToString() == "D") {
        discountTotal += Convert.ToDecimal(dgvPOScart.Rows[i].Cells[5].Value);
      }
    }
    
    totaldue.Text = string.Format("{0}", cartTotal - (discountTotal * .2M));
    subtotal.Text = string.Format("{0}", cartTotal);
    totaldiscountablesales.Text = string.Format("{0}", discountTotal);
    discount.Text = string.Format("{0}", discountTotal * .2M);
    
    decimal cartotal=0;
    小数折扣合计=0;
    对于(int i=0;i

    在处理货币价值时,你应该使用十进制而不是双精度,因为精度很重要。

    青霉素不是这样拼写的。我知道,很抱歉:如果你告诉我们哪些数字是对的,哪些是错的,vIt可能会有所帮助,如果它们是错的,数字应该是什么<代码>小计仍然应该是所有价格的总和,无论是否使用D
    ,但看起来您只增加了else if块中的“sum”变量。编辑它谢谢you@Anderson谢谢你,先生,我把它编辑成=“谢谢你,先生,它工作得很好,你的代码更有效。如果你不介意我问先生,我怎样才能把价格的格式改为0.00?因为它显示了0.000。再次感谢您。@FutureDev您可以使用string.format以一百万种方式格式化它。要只保留两位小数,请尝试
    subtotal.Text=string.Format(“{0:0.00}”,cartotal)decimal cartTotal = 0;
    decimal discountTotal = 0;
    for (int i = 0; i < dgvPOScart.Rows.Count; ++i) {
      cartTotal += Convert.ToDecimal(dgvPOScart.Rows[i].Cells[5].Value);
      if (cbDiscount2.Checked && dgvPOScart.Rows[i].Cells[6].Value.ToString() == "D") {
        discountTotal += Convert.ToDecimal(dgvPOScart.Rows[i].Cells[5].Value);
      }
    }
    
    totaldue.Text = string.Format("{0}", cartTotal - (discountTotal * .2M));
    subtotal.Text = string.Format("{0}", cartTotal);
    totaldiscountablesales.Text = string.Format("{0}", discountTotal);
    discount.Text = string.Format("{0}", discountTotal * .2M);