none
How to Check if a value exist in a datagridview row and delete it.

    Question

  • How to Check if a value exist in a datagridview and if true; delete the whole row ?

     c# example please


    karel
    Tuesday, January 25, 2011 8:03 PM

Answers

  • Hi sjaak12,

    Is the dataGridView databound? If so, you may do the check in the dataset which is locally. Otherwise the only way is to loop through all the cells to check if the value is existed like this:

              //check if the value from textBox1 is existed in dataGridView1:

                for(int i=0; i<dataGridView1.Rows.Count; i++)

                {

                    for (int j = 0; j < dataGridView1.Columns.Count; j++)

                    {

                        if (dataGridView1.Rows[i].Cells[j].Value!=null && textBox1.Text == dataGridView1.Rows[i].Cells[j].Value.ToString())

                        {

                            MessageBox.Show("The value already existed in DataGridView.");

                             //do your deleting here if th dataGridView is not bound, or delete the row in dataTable(which is the datasource) and then update the dataGridView. 

                            break;

                        }

                    }

                }

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by sjaak12 Wednesday, January 26, 2011 9:47 PM
    Wednesday, January 26, 2011 5:35 AM

All replies

  • Hi sjaak12,

    Is the dataGridView databound? If so, you may do the check in the dataset which is locally. Otherwise the only way is to loop through all the cells to check if the value is existed like this:

              //check if the value from textBox1 is existed in dataGridView1:

                for(int i=0; i<dataGridView1.Rows.Count; i++)

                {

                    for (int j = 0; j < dataGridView1.Columns.Count; j++)

                    {

                        if (dataGridView1.Rows[i].Cells[j].Value!=null && textBox1.Text == dataGridView1.Rows[i].Cells[j].Value.ToString())

                        {

                            MessageBox.Show("The value already existed in DataGridView.");

                             //do your deleting here if th dataGridView is not bound, or delete the row in dataTable(which is the datasource) and then update the dataGridView. 

                            break;

                        }

                    }

                }

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by sjaak12 Wednesday, January 26, 2011 9:47 PM
    Wednesday, January 26, 2011 5:35 AM
  • Thank you Helen. 

    it helped a lot !

    Wednesday, January 26, 2011 9:40 PM