简体   繁体   中英

How can I search for a given string in a dataGridView?

I have a dataGridView1 and the User can enter infos to it, then by clicking a button3 I want him to search for whatever he types in a textBox3 And to get a MessageBox saying if the string was found or not in the datagridview.

This is my code

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!!");
        }

    }
    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!!");
    }

Someone sent you a link to another similar answer, so I answered the question there by mistake lol..

Anyways, this method will help you retreive the DataGridViewCell object in which the text was found. I did not really test this code

    /// <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
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM