在表单加载时加载datagridview的线程(显示错误) [英] Thread to load datagridview on form load(showing error)

查看:52
本文介绍了在表单加载时加载datagridview的线程(显示错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们......



我需要将一些数据加载到datagridview.Its很少耗时。所以我决定开始一个主题。所以我在表单加载事件和调用耗时函数上创建了一个线程实例。但它抛出一个错误

跨线程操作无效:从线程以外的线程访问控件它创建于。



我的代码如下。

在表单加载事件中

线程primarythrd =  new 线程(primaryload); 
primarythrd.IsBackground = true ;
primarythrd.SetApartmentState(System.Threading.ApartmentState.STA);
primarythrd.Start();





primaryload function

  public   void  primaryload()
{
// 这行代码会出错。
dataGridView1.Rows.Add(item,malecount,femalecount,malecount + femalecount, Math.Round(calc, 0 ));
}





如何解决此错误。什么是将大量数据加载到UI而不会篡改性能的最佳方法。

解决方案

我建​​议创建一个委托来仅调用数据网格视图上的方法。



一个注意事项:列至少需要存在(或者在行之前的调用中首先创建)。



  private   void 加载(){
线程primarythrd = new 线程(primaryload);
primarythrd.IsBackground = true ;
primarythrd.SetApartmentState(System.Threading.ApartmentState.STA);
primarythrd.Start();
}

委托 void AddRows(DataGridView view);
public void primaryload()
{
// 这行代码会出错。
// dataGridView1.Rows.Add(item,malecount,femalecount,malecount + femalecount,//Math.Round(calc,0));
AddRows addRows = new AddRows(DoRowAdd);
this .Invoke(addRows,dataGridView1);
}

private void DoRowAdd(DataGridView view){
if (view.InvokeRequired){
AddRows rows = new AddRows(DoRowAdd) ;
this .Invoke(rows, new object [] {view});
}
其他 {
for int i = 0 ; i < 100 ; i ++){
view.Rows.Add(item,malecount,femalecount,malecount + femalecount, // Math.Round(calc,0));
}
}
}


此代码块帮助我解决问题

  .Invoke( (MethodInvoker) delegate  
{
this .dataGridView1.Rows.Add(item ,malecount,femalecount,malecount + femalecount,Math.Round(calc, 0 ));
});


Hi Friends...

I need to load some datas to datagridview.Its little time consuming. so i decide to start a thread. so i created a thread instance on form load event and call time consuming function.But its throwing an error
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

My code is as follows.
In the form load event

Thread primarythrd = new Thread(primaryload);
                primarythrd.IsBackground = true;
                primarythrd.SetApartmentState(System.Threading.ApartmentState.STA);
                primarythrd.Start();



primaryload function

public void primaryload()
        {
//this line of code gives error.
dataGridView1.Rows.Add(item, malecount, femalecount, malecount + femalecount, Math.Round(calc, 0));
}



How can i solve this error. whats the best method to load huge data to UI without tampering the performance.

解决方案

I would suggest creating a delegate to invoke a method on just the data grid view.

One note: The columns at least need to be present (or created first in that invoke before the rows).

private void Load() {
Thread primarythrd = new Thread(primaryload);
    primarythrd.IsBackground = true;
    primarythrd.SetApartmentState(System.Threading.ApartmentState.STA);
    primarythrd.Start();
}

delegate void AddRows(DataGridView view);
public void primaryload()
        {
//this line of code gives error.
//dataGridView1.Rows.Add(item, malecount, femalecount, malecount + femalecount, //Math.Round(calc, 0));
AddRows addRows = new AddRows(DoRowAdd);
this.Invoke(addRows, dataGridView1);
}

private void DoRowAdd(DataGridView view) {
    if (view.InvokeRequired) {
        AddRows rows = new AddRows(DoRowAdd);
        this.Invoke(rows, new object[] { view });
    }
    else {
        for (int i = 0; i < 100; i++) {
            view.Rows.Add(item, malecount, femalecount, malecount + femalecount, //Math.Round(calc, 0));
        }
    }
}


This Code block help me to solve the issue

this.Invoke((MethodInvoker)delegate
            {
                this.dataGridView1.Rows.Add(item, malecount, femalecount, malecount + femalecount, Math.Round(calc, 0));
            });


这篇关于在表单加载时加载datagridview的线程(显示错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆