Dinesh Soni
Dinesh Soni

Reputation: 19

How to find if item already present in the datagridview

I am trying to find if the item i want to add is already present in the datagridview or not ?

if (dgvIndex >= 1)
{
    foreach (dataGridView1 row in dataGridView1.Rows)
    {
        if (row.cells[1].value == dr[1].ToString())
        {
            MessageBox.Show("Item already added");
        }
    }
}

is is correct ?

I am getting an error: datagridview1 is a 'field' but is used like a 'type'.

Upvotes: 0

Views: 797

Answers (1)

adv12
adv12

Reputation: 8551

The foreach keyword expects you to say something like:

foreach(DataGridViewRow row in dataGridView1.Rows)

The DataGridView is for specifying the type of the row variable. The compiler is telling you that dataGridView1 is not a type.

I think you have some other syntax errors to fix, like the casing on row.cells[1].value. In any case, hopefully this gets you a bit closer to compiling.

Upvotes: 3

Related Questions