我有一个应该由线程重新加载的DataGridView。但是当线程执行时,DataGridView会闪烁并且不会加载更改。
有趣的是,如果我在线程上尝试代码而不在线程上(在主线程上执行),DataGridView会重新加载!
这是我的代码:
public void reloadClients(object source, EventArgs e)
{
//Console.WriteLine("Clients will reload.");
dgvClientes.DataSource = null;
loadReloadClients = new Thread(new ThreadStart(jobReloadAllClients));
loadReloadClients.Start();
}
private void jobReloadAllClients()
{
this.Invoke((MethodInvoker)delegate
{
descricaoCliente.Text = "CARREGANDO TODOS OS CLIENTES";
});
System.Windows.Forms.BindingSource bs = new BindingSource();
bs.DataSource = clientes.allClients().Tables["Clientes"];
this.Invoke((MethodInvoker)delegate
{
descricaoCliente.Text = "Todos os Clientes";
dgvClientes.DataSource = bs;
dgvClientes.Refresh();
});
}
我被告知在主线程上设置绑定数据源并在后台线程上加载BD数据.... Sievajet这是我到目前为止所得到的......
System.Windows.Forms.BindingSource bs;
public void reloadClients(object source, EventArgs e)
{
//Console.WriteLine("Clients will reload.");
dgvClientes.DataSource = null;
bs = new BindingSource();
dgvClientes.DataSource = bs;
loadReloadClients = new Thread(new ThreadStart(jobReloadAllClients));
loadReloadClients.Start();
}
private void jobReloadAllClients()
{
this.Invoke((MethodInvoker)delegate
{
descricaoCliente.Text = "CARREGANDO TODOS OS CLIENTES";
});
DataSet Setter = clientes.allClients();
this.Invoke((MethodInvoker)delegate
{
bs.DataSource = Setter.Tables["Clientes"];
descricaoCliente.Text = "Todos os Clientes";
//dgvClientes.DataSource = bs;
//dgvClientes.Refresh();
});
}
答案 0 :(得分:0)
我做到了......出于某种原因,我不得不释放变量bs,然后再次初始化它......现在它工作正常。
System.Windows.Forms.BindingSource bs;
public void reloadClients(object source, EventArgs e)
{
//Console.WriteLine("Clients will reload.");
bs = null;
dgvClientes.DataSource = null;
bs = new BindingSource();
dgvClientes.DataSource = bs;
loadReloadClients = new Thread(new ThreadStart(jobReloadAllClients));
loadReloadClients.Start();
}
private void jobReloadAllClients()
{
this.Invoke((MethodInvoker)delegate
{
descricaoCliente.Text = "CARREGANDO TODOS OS CLIENTES";
});
DataSet Setter = clientes.allClients();
this.Invoke((MethodInvoker)delegate
{
bs.DataSource = Setter.Tables["Clientes"];
descricaoCliente.Text = "Todos os Clientes";
});
}