So, I have a data grid view that needs to be updated in real-time, for a management software. I'm trying to update the control on a separate thread, but it throws an exception:
Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on.
Here is the code:
public void atualiza()
{
for (int i = 0; ; i++)
{
i--;
tabela();
System.Threading.Thread.Sleep(90000);
}
}
private void mainUser_Load(object sender, EventArgs e)
{
atualizaTabela t = new atualizaTabela(atualiza);
t.BeginInvoke(null, null);
}
"Tabela" is the method that actually updates the data grid view:
public void tabela()
{
string comando = "SELECT * FROM Job";
OracleDataAdapter da;
OracleCommand comm;
DataSet ds;
OracleConnection Conn = new OracleConnection(dbstring);
comm = new OracleCommand(comando, Conn);
da = new OracleDataAdapter(comm);
ds = new DataSet();
da.Fill(ds, "Job");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Job";
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.Columns[0].HeaderText = "Id";
dataGridView1.Columns[1].HeaderText = "Produto";
dataGridView1.Columns[2].HeaderText = "Cliente";
dataGridView1.Columns[3].HeaderText = "Data de Pedido";
dataGridView1.Columns[4].HeaderText = "Previsão de Entrega";
dataGridView1.Columns[5].HeaderText = "Preço";
dataGridView1.Columns[6].HeaderText = "Tamanho";
dataGridView1.Columns[7].HeaderText = "Quantidade";
dataGridView1.Columns[8].HeaderText = "Comentário";
}
"Atualizatabela" is a delegate. The exception is thrown when I try to bind the datagridview to the datasource: (dataGridView1.DataSource = ds)