datagridview搜索未显示正确答案

时间:2018-10-04 00:36:00

标签: c# datagridview

按下搜索按钮时,我会尝试计算有多少个结果具有相同的“用户名”,然后将其“收入”相加(收入为十进制)

private void button5_Click(object sender, EventArgs e)
    {
        string searchValue = textBox5.Text;
        int rowIndex = 1; 

        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        try
        {
            bool valueResult = true;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[1].Value.ToString().Equals(searchValue))
                {
                    rowIndex = row.Index;
                    dataGridView1.Rows[1].Selected = true;
                    rowIndex++;
                    valueResult = false;
                    var count = this.dataGridView1.Rows.Cast<DataGridViewRow>()
                   .Count(roww => row.Cells[1].Value.ToString() == searchValue);
                    this.textBox6.Text = count.ToString();
                }
            }
            if (valueResult != false)
            {
                MessageBox.Show("Record is not avalable for this Name: " + textBox5.Text, "Not Found");
                return;
            }
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }

1)但是出于某种原因,textbox6会在“ id”列中显示最后一个数字,而不管搜索的用户名如何 2)另外,我无法弄清楚如何将搜索到的特定用户的“收入”中的每个单元格相加

[Example of datagridview from MySql] 1

我仍然是C#的初学者

1 个答案:

答案 0 :(得分:0)

由于您已经使用foreach遍历了行,因此无需每次都在foreach中重新计算匹配计数。相反,您可以增加一个计数器,然后将所有匹配的行的收入总计加起来。

textbox6似乎具有最后一行的“ id”列(实际上是网格中所有行的计数)的值的原因是由于Count条件:

.Count(roww => row.Cells[1].Value.ToString() == searchValue)

应为:

.Count(roww => roww.Cells[1].Value.ToString() == searchValue)

使用roww => row作为它,并且在if条件之内,它将返回所有行的计数,而不仅仅是匹配的行。但是同样,.Count是不必要的。

这是经过修改的版本,它遍历各行并增加一个计数器,并增加与选择条件相匹配的行的总收入:

    private void button5_Click(object sender, EventArgs e)
    {
        string searchValue = textBox5.Text;

        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        try
        {
            decimal earnedTotal = 0;
            int matches = 0;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if(!row.IsNewRow)
                {
                    if((string)row.Cells[1].Value == searchValue)
                    {
                        row.Selected = true;

                        decimal earned;
                        if (decimal.TryParse((string)row.Cells[2].Value, out earned))
                            earnedTotal += earned;

                        matches++;
                    }
                    else
                    {
                        row.Selected = false;
                    }
                }
            }

            if(matches == 0)
                MessageBox.Show("Record is not avalable for this Name: " + textBox5.Text, "Not Found");

            textBox6.Text = matches.ToString();
            txtEarnedTotal.Text = earnedTotal.ToString();
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }