gpt4 book ai didi

c# - 填充 dataGridView 感谢 backGroundWorker

转载 作者:太空狗 更新时间:2023-10-29 22:34:54 35 4
gpt4 key购买 nike

我有这段代码:

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
remplirDataGrid();
}

private void frmChercherActesLoad(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();

}



private void remplirDataGrid()
{
dataGridView1.DataSource = ActeServices.getAllActes(0, 40);
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].HeaderText = "Code acte";
dataGridView1.Columns[2].HeaderText = "Désignation";
dataGridView1.Columns[3].HeaderText = "Pris en charge";
dataGridView1.Columns[4].HeaderText = "Id article";
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}

这里是方法getAllActe:

public static IEnumerable<Acte> getAllActes(int skipCount, int takeCount)
{
var myTableAdapter = new SmartDocDALServices.SmartDocDataSetTableAdapters.actesTableAdapter();
myTableAdapter.Fill(myDataSet.actes);
var myResult = from q in myDataSet.actes.AsEnumerable()
select new Acte
{
code = q.code,
designation = q.designation,
priseEnCharge = q.prise_en_charge,
idArticle = q.id_article,
};

if (skipCount != -1)
myResult.Skip(skipCount);
if (takeCount != -1)
myResult.Take(takeCount);
IEnumerable<Acte> myResultRet = myResult.ToList();
return myResultRet;

我喜欢做的是在我运行应用程序后使用后台工作程序填充我的 datagridview 我收到此错误:

Inter-thread operation not valid: Control 'dataGridView1 has been the subject of an access from a thread other than the one it was created.


我试试这个

 private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
IEnumerable<Acte> result = ActeServices.getAllActes(0, 40);
backgroundWorker1.ReportProgress(0, result);

}

void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{

dataGridView1.DataSource = (IEnumerable<Acte>)e.UserState;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].HeaderText = "Code acte";
***

但时间一无所获?我希望当 BGW 加载数据时数据网格更新 foreash 数据加载它将它添加到 DGV

最佳答案

您不能从 BackgroundWorker 线程更新 UI。

你需要向 UI 发送一个事件,然后有类似的东西:

private void EventHandler(object sender, YourEventArgs e)
{
if (this.dataGridView1.InvokeRequired)
{
this.dataGridView1.Invoke((MethodInvoker)delegate { this.AddToGrid(e.YourData); });
}
else
{
this.AddToGrid(e.YourData);
}
}

关于c# - 填充 dataGridView 感谢 backGroundWorker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127838/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com