【问题标题】:How to sum datagridview selected cell CORRECTLY如何正确汇总datagridview选定的单元格
【发布时间】:2019-03-05 17:28:12
【问题描述】:

它工作正常但不正确 ...如果从上到下选择它的总和是正确的,但是从下到上选择不求和

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


            if (float.TryParse(SalesGridView.SelectedCells[i].FormattedValue.ToString(), out nextNumber))
                sumNumbers += nextNumber;


           label1.Text = "selected value " + sumNumbers;
            label2.Text = "nr selected cells " + SalesGridView.SelectedCells.Count.ToString();
        }
        else
        {

        }
    }
}

【问题讨论】:

  • 你不能在这里使用Computethis example 一样吗?这种方法似乎是“风景路线”。我不是 100% 确定您所说的“从上到下”和“从下到上”是什么意思,但是如果您根据记录的顺序得到不同的值,那么我猜您没有对正确的行求和。
  • 我想这个问题是关于 winforms 的?
  • 是的winforms datagridview

标签: c# winforms datagridview


【解决方案1】:

试试下面的代码:

private void SalesGridView_SelectionChanged(object sender, EventArgs e)
{
    float sumNumbers = 0;
    for (int i = 0; i < SalesGridView.SelectedCells.Count; i++)
    {
        var selectedCell = SalesGridView.SelectedCells[i];
        if (selectedCell.ColumnIndex == SalesGridView.Columns["TotalBillColumn"].Index)
        {
            float nextNumber = 0;

            if (float.TryParse(selectedCell.FormattedValue.ToString(), out nextNumber))
                sumNumbers += nextNumber;
        }
    }

    label1.Text = "selected value " + sumNumbers;
    label2.Text = "nr selected cells " + SalesGridView.SelectedCells.Count.ToString();
}

对不起,对于迟到的解释,当我写这个答案时,没有时间详细说明你的实际问题是什么,只给了你一个应该可行的解决方案。

您的问题可以定位在该行:

if (!SalesGridView.SelectedCells.Contains(SalesGridView.Rows[i].Cells["TotalBillColumn"]))

准确地说,以下陈述会给你错误的结果:

SalesGridView.Rows[i]

您将收到不需要的结果,因为i 不符合您对Rows 索引的要求,所以让我们看看您在for 循环中写了什么。

for (int i = 0; i < SalesGridView.SelectedCells.Count; i++)

您的for 循环尝试枚举您的SalesGridViewSelectedCells

假设您选择 5 个单元格,而不是您的 for 循环,枚举从 0 到独占 5,所以我将是 0, 1, 2, 3 or 4

现在让我们再次检查一下我之前提到的有问题的陈述:

SalesGridView.Rows[i]

SelectedCelli = 0 是否对应于 Rows[0] 不,它不必。

您的SelectedCells 可以是任何行,并且您的for 循环将始终从0 枚举到SelectedCells.Count,但您的SalesGridView.Rows[i] 语句将始终尝试使用给定索引获取Row i.

所以,例如:

  1. 您在67 行中选择2 单元格。
  2. SelectedCells.Count 将有 2
  3. 您从0 开始枚举SelectedCells 到唯一的2,所以i 将是01
  4. 在您的循环中,您尝试通过调用SalesGridView.Rows[i] 访问带有i 的行
  5. 3.i 中所述,将仅是01,但我选择了2 行的67 单元格

【讨论】:

  • throws ex 属性或索引器“DataGridViewCell.ColumnIndex”无法分配给--它是只读的............但它没有设置为只读。
  • 抱歉,打错了,只写了=而不是==
【解决方案2】:
private void button1_Click(object sender, EventArgs e)
{
    double result = 0;
    foreach (DataGridViewCell  cell in dataGridView1.SelectedCells)
    {
        result = result + double.Parse(cell.Value.ToString());
    }
    label2.Text = result.ToString();

}

如果您想安全起见,请使用 TryParse:

private void button1_Click(object sender, EventArgs e)
{
    double result = 0;
    double nextNumber = 0;
    foreach (DataGridViewCell  cell in dataGridView1.SelectedCells)
    {
        if (double.TryParse(cell.Value.ToString(), out result)) 
        {
            nextNumber += result;
            label2.Text = nextNumber.ToString();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2023-02-10
    • 2011-08-21
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多