从线程向DataGridView添加行 [英] Adding row to DataGridView from Thread

查看:81
本文介绍了从线程向DataGridView添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从两个单独的线程向DataGridView添加行.我尝试了一些与委托和BeginInvoke的操作,但是不起作用.

I would like to add rows to DataGridView from two seperate threads. I tried something with delegates and BeginInvoke but doesn't work.

这是我的行更新器函数,它是从线程中的另一个函数调用的.

Here is my row updater function which is called from another function in a thread.

    public delegate void GRIDLOGDelegate(string ulke, string url, string ip = "");
    private void GRIDLOG(string ulke, string url, string ip = "")
    {

        if (this.InvokeRequired)
        {
            // Pass the same function to BeginInvoke,
            // but the call would come on the correct
            // thread and InvokeRequired will be false.
            object[] myArray = new object[3];

            myArray[0] = ulke;
            myArray[1] = url;
            myArray[2] = ip;

            this.BeginInvoke(new GRIDLOGDelegate(GRIDLOG),
                                             new object[] { myArray });

            return;
        }

        //Yeni bir satır daha oluştur
        string[] newRow = new string[] { ulke, url, ip };
        dgLogGrid.Rows.Add(newRow);
    }

推荐答案

this.BeginInvoke(new GRIDLOGDelegate(GRIDLOG),
        //error seems to be here -> new object[] { myArray });
        myArray) // <- how it should be

更新:

您也可以这样操作:

BeginInvoke(new GRIDLOGDelegate(GRIDLOG), ulke, url, ip);

这篇关于从线程向DataGridView添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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