C#添加列值并输出到标签

时间:2016-07-21 03:01:46

标签: c# datagridview

我试图获取一些标签来输出加载到datagridview中的列的总和。

这就是我放在一起的内容,在调试它时,它会对第一个规则进行正确的数学计算,然后跳转其余的代码,从不读取输出代码并继续加载表单。

不确定是否重要但不是每列中的所有值都是数值(DateTime和字符串都在其他列中),我肯定行[]数字对于我需要的数据是正确的。< / p>

        LoadData();

        // ISSUE STARTS HERE
        int PartsTotal = 0;
        int SuppliesTotal = 0;
        int ReplacementTotal = 0;
        int RecycleTotal = 0;
        int ReturnTotal = 0;

        for (int i = 0; i < dataGridView1.Rows.Count; ++i)
        {
            PartsTotal += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
            SuppliesTotal += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
            ReplacementTotal += Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
            RecycleTotal += Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value);
            ReturnTotal += Convert.ToInt32(dataGridView1.Rows[i].Cells[6].Value);
        }

        lblPartsTotal.Text = PartsTotal.ToString("c");
        lblPartsTotal.Visible = true;

        lblPartsTotal.Text = PartsTotal.ToString("c");
        lblPartsTotal.Visible = true;

        lblReplacementTotal.Text = ReplacementTotal.ToString("c");
        lblReplacementTotal.Visible = true;

        lblRecycleTotal.Text = RecycleTotal.ToString("c");
        lblRecycleTotal.Visible = true;

        lblReturnsTotal.Text = ReturnTotal.ToString("c");
        lblReturnsTotal.Visible = true;

0 个答案:

没有答案