【问题标题】:How can I search for a given string in a dataGridView?如何在 dataGridView 中搜索给定的字符串?
【发布时间】:2017-02-15 16:07:09
【问题描述】:

我有一个 dataGridView1,用户可以向其中输入信息,然后通过单击按钮 3,我希望他搜索他在 textBox3 中键入的任何内容,并获得一个消息框,说明是否在 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!!");
        }

    }

【问题讨论】:

  • 我按下button3时出错!
  • 针对您作为答案发布的问题:您必须在对其执行 ToString() 之前检查 Cells[i].Value 是否为空。 ToString() 不能处理空值。
  • 另外,只是因为挑剔,您可能不想使用 j 作为布尔变量的名称。人们经常使用 j 作为 int 来遍历集合,它看起来很奇怪。将找到或存在更好的名称。

标签: c#


【解决方案1】:
    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!!");
    }

【讨论】:

    【解决方案2】:

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

    无论如何,此方法将帮助您检索在其中找到文本的 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
            }
        }
    

    【讨论】:

    • 它总是编译它在网格中不存在的情况,即使它存在!
    猜你喜欢
    • 2012-03-23
    • 2017-12-02
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    相关资源
    最近更新 更多