Good morning everyone, I'm having trouble with rewriting a DataGrid, the function works, but I would need to do a faster search, so the thought was to add parallelism.
But upon applying the latter it generates an error for me: System.InvalidOperationException: 'Invalid cross-thread operation: the control 'dataGridView1' was accessed from a different thread than the one from which the creation was performed.'
The problem is clear to me, however I can't figure out how to solve it. Could you guys please help me out?
I've already tried applying Invoke but the program goes into an infinite loop.
private void inputSearch_TextChanged(object sender, EventArgs e)
{
Parallel.For(0, 7, i =>
{
Ricerca(i);
});
}
private void Ricerca(int i)
{
string searchValue = inputSearch.Text.ToUpper();
var re = from row in dataTable.AsEnumerable()
where
row[i].ToString().Contains(searchValue)
select row;
if (re.Count() != 0)
{
Invoke(new Action(() =>
{
dataGridView1.DataSource = re.CopyToDataTable();
dataGridView1.Columns[7].Visible = false;
}));
}
}