gpt4 book ai didi

c# - 如何在c#中创建访问datagridview控件的委托(delegate)方法?

转载 作者:行者123 更新时间:2023-12-04 06:43:22 24 4
gpt4 key购买 nike

m 有一个 winForm 并且在那个 m 中使用 BackGroundWorker用于保持表单 GUI 事件的控件。
现在我从 backgroundworker_doWork() 访问 datagridview方法,所以我在下面创建了一个委托(delegate)方法:

    delegate void updateGridDelegate();
private void invokeGridControls()
{
if (autoGridView.InvokeRequired)
{
updateGridDelegate delegateControl = new updateGridDelegate(invokeGridControls);
autoGridView.Invoke(delegateControl);//here i need to do something to access autoGridView.Rows.Count
}
}

backgroundworker_DoWork()事件 m 访问 datagridview 为
int temp2noofrows = autoGridView.Rows.Count - 1;// here i dn't understand how to call my delegate method, so i can avoid cross threading access error

最佳答案

尝试使用操作代表
并假设您使用的是 .net 2.0 及更高版本

 autoGridView.Invoke(
new Action(
delegate()
{
int temp2noofrows = autoGridView.Rows.Count - 1;//
}
)
);

关于c# - 如何在c#中创建访问datagridview控件的委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3975636/

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