【问题标题】:summing selected values of datagridview对 datagridview 的选定值求和
【发布时间】:2010-12-06 07:44:47
【问题描述】:

我将此代码用于汇总选定的单元格。它的工作很好,但是当用户选择单元格中的字母时会抛出异常:) 当在选择单元格中时,我如何确保字母不求和

我问了同样的问题,但我无法添加我的代码

 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
        {
            if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[i].Cells["cLoadName"]))
            {
                float nextNumber = 0;
                float sumNumbers = 0;

                                        nextNumber = float.Parse(dataGridView1.SelectedCells[i].FormattedValue.ToString());
                    sumNumbers += nextNumber;


                tsslSumSelected.Text = "ჯამი: " + sumNumbers;
                tsslTotalSelected.Text = "მონიშნული: " + dataGridView1.SelectedCells.Count.ToString();
            }
            else
            {

            }
        }    

    }

【问题讨论】:

    标签: c# datagrid


    【解决方案1】:
    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
            {
                if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[i].Cells["cLoadName"]))
                {
                    float nextNumber = 0;
                    float sumNumbers = 0;
    
                        if (float.TryParse(dataGridView1.SelectedCells[i].FormattedValue.ToString(), out nextNumber))
                            sumNumbers += nextNumber;
    
    
                    tsslSumSelected.Text = "ჯამი: " + sumNumbers;
                    tsslTotalSelected.Text = "მონიშნული: " + dataGridView1.SelectedCells.Count.ToString();
                }
                else
                {
    
                }
            }    
    
        }
    

    【讨论】:

    • 它对我不起作用。如果所选单元格包含不求和的字母,您能否简单地给出仅对数字求和的代码
    • 这就是代码应该做的事情。我检查单元格是否包含有效数字。为什么它不适合你?
    【解决方案2】:
    public void ComputeGridTot()
    {
        try
        {
            decimal qty= 0;
            decimal price = 0;
            decimal tax = 0;
            decimal Total = 0;
            for (int i = 0; i < drGrdView1.Rows.Count; i++)
            {
                if (drGrdView1.Rows[i].Cells[7].Value.ToString() == blcnt.ToString())
                {
                    qty += Convert.ToDecimal(drGrdView1.Rows[i].Cells[3].Value);
                    price += Convert.ToDecimal(drGrdView1.Rows[i].Cells[4].Value);
                    tax += Convert.ToDecimal(drGrdView1.Rows[i].Cells[5].Value);
                    Total += Convert.ToDecimal(drGrdView1.Rows[i].Cells[6].Value);
    
                 }
            }
            lblGTax.Text = tax.ToString();
            lblGQty.Text = qty.ToString();
            lblGPrice.Text = price.ToString();
            lblGTot.Text = Total.ToString();
        }
        catch
        {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多