ISSUE :
 
Profile photo
Submitted by FlyTrap
on June 16th, 2011 at 23:41:52 EST

Updating dataGridView from different thread than UI thread

How do i update dataGridView from seperate thread than UI thread. I get runtime exception when i update datagridview from seperate thread.
Any suggestion ..


Profile photo
Replied by FlyTrap
on June 16th, 2011 at 23:47:19 EST
I get following error

Cross-thread operation not valid: Control 'dgvCustomerAddress' accessed from a thread other than the thread it was created on.

Profile photo
Replied by Victor Failing
on June 16th, 2011 at 23:57:08 EST
You can create a handler for updating your dataGridView and use beginInvoke for datagridview and pass your datasource as parameter . I create following sample . hope
this helps .

Code sample before using Handler

delegate void UpdateGridHandler(DataTable table);
public partial class Form1 : Form
{
public Form1()

{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
UpdateGridHandler ug = UpdateGrid;
ug.BeginInvoke(dt, cb, null);
}

private void cb(IAsyncResult res)
{

}

private void UpdateGrid(DataTable table)
{
dataGridView1.DataSource = table;
}


}


____________________________________________________________________________


Thread safe code Sample

delegate void UpdateGridHandler(DataTable table);
delegate void UpdateGridThreadHandler(DataTable table);
public partial class Form1 : Form
{
public Form1()

{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
dt.Rows.Add("victor");
UpdateGridHandler ug = UpdateGrid;
ug.BeginInvoke(dt, cb, null);
}

private void cb(IAsyncResult res)
{

}

private void UpdateGrid11(DataTable table)
{
dataGridView1.DataSource = table;
}

private void UpdateGrid(DataTable table)
{
if (dataGridView1.InvokeRequired)
{
UpdateGridThreadHandler handler = UpdateGrid11;
dataGridView1.BeginInvoke(handler, table);
}
else
{
dataGridView1.DataSource = table;
}
}
Accepted Answer
Accepted
 Answer

Profile photo
Replied by FlyTrap
on June 16th, 2011 at 23:59:39 EST
This works great even though im still confused how this is working :)

Profile photo
Replied by FlyTrap
on June 17th, 2011 at 00:01:02 EST
Thanks Victor !!

Profile photo
Replied by Lalita P
on March 18th, 2013 at 10:08:59 EST
Thank you.
It works fine.
It also work fine with my real time example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
delegate void UpdateGridHandler(DataTable table);
delegate void UpdateGridThreadHandler(DataTable table);
clsSycronousMethodCall obj = new clsSycronousMethodCall();

DataTable dt = new DataTable();
public Form2()
{
InitializeComponent();
}

private void cmdLoad_Click(object sender, EventArgs e)
{

dt = obj.GetItemList(Convert.ToInt32(4));

UpdateGridHandler ug = UpdateGrid;
ug.BeginInvoke(dt, cb, null);

}
private void cb(IAsyncResult res)
{

}
private void UpdateGrid11(DataTable table)
{
dataGridView1.DataSource = table;
}

private void UpdateGrid(DataTable table)
{
if (dataGridView1.InvokeRequired)
{
UpdateGridThreadHandler handler = UpdateGrid11;
dataGridView1.BeginInvoke(handler, table);
}
else
{
dataGridView1.DataSource = table;
}
}



private void Form2_Load(object sender, EventArgs e)
{

}
}
}

Profile photo
Replied by Evan Croby
on July 22nd, 2014 at 02:01:35 EST