I’m using a foreach loop to populate each row in a DataGridView with a string. I need to search the DataGridView to make sure that I don’t add a string that is already there. What is the best way to do this?
Here is my code so far:
foreach (String file in openFileDialog.FileNames)
{
// todo: make sure file string does not already exist in DataGridView
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[1].Value = file;
i++;
}
Please note that there may already be file names in the DataGridView from a previous run of the code.
It is a good idea not to use a DataGridView as a datastore. It is a Control for displaying stuff.
It is better to bind it to some backing store and do your operations on that:
And when new a new batch of files comes in, add them to the HashSet and simply re-bind the Grid.