如何在dataGridView中搜索给定的字符串?

时间:2017-02-15 16:07:09

标签: c#

我有一个dataGridView1,用户可以输入信息,然后点击一个button3,我希望他在textBox3中搜索他输入的内容,并获得一个MessageBox,说明是否在datagridview中找到了字符串。

这是我的代码

private void button3_Click(object sender, EventArgs e)
    {
        bool j = false;
        foreach (DataGridViewRow rows in dataGridView1.Rows)
        {

            for (int i = 1; i < rows.Cells.Count; i++)
            {
                if(j == false)
                {
                    if (textBox3.Text == rows.Cells[i].Value.ToString())
                    {
                        j = true;
                    }
                }
                else
                {
                    break;
                }


            }

        }



        if (j == true)
        {
            MessageBox.Show("It exists!");
        }
        else
        {
            MessageBox.Show("It doesn't exist!!");
        }

    }

2 个答案:

答案 0 :(得分:0)

    bool j = false;
    foreach (DataGridViewRow rows in dataGridView1.Rows)
    {
        for (int i = 1; i < rows.Cells.Count; i++)
        {
            if (textBox3.Text == rows.Cells[i].Value.ToString())
            {
                j = true;
                break; // No need to continue after finding the result
            }
        }
    }

    if (j) // j is already a boolean
    {
        MessageBox.Show("It exists!");
    }
    else
    {
        MessageBox.Show("It doesn't exist!!");
    }

答案 1 :(得分:0)

有人给你发了另一个类似答案的链接,所以我错误地回答了那个问题大声笑..

无论如何,此方法将帮助您检索找到文本的DataGridViewCell对象。我没有真正测试这段代码

    /// <summary>
    /// Check if a given text exists in the given DataGridView
    /// </summary>
    /// <param name="searchText"></param>
    /// <param name="dataGridView"></param>
    /// <returns>The cell in which the searchText was found</returns>
    private DataGridViewCell GetCellWhereTextExistsInGridView(string searchText, DataGridView dataGridView)
    {
        DataGridViewCell cellWhereTextIsMet = null;

        // For every row in the grid (obviously)
        foreach (DataGridViewRow row in dataGridView.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                // I did not test this case, but cell.Value is an object, and objects can be null
                // So check if the cell is null before using .ToString()

                if (cell.Value != null && searchText == cell.Value.ToString())
                {
                    // the searchText is equals to the text in this cell.
                    cellWhereTextIsMet = cell;
                    break;
                }
            }
        }

        return cellWhereTextIsMet;
    }

    private void button_click(object sender, EventArgs e)
    {
        DataGridViewCell cell = GetCellWhereTextExistsInGridView(textBox1.Text, myGridView);
        if (cell != null)
        {
            // Value exists in the grid
            // you can do extra stuff on the cell
            cell.Style = new DataGridViewCellStyle { ForeColor = Color.Red };
        }
        else
        {
            // Value does not exist in the grid
        }
    }