【问题标题】:Adding row to DataGridView from Thread从线程向 DataGridView 添加行
【发布时间】:2010-06-06 23:03:15
【问题描述】:

我想从两个单独的线程向 DataGridView 添加行。我用代表和 BeginInvoke 尝试了一些方法,但没有用。

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

    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);
    }

【问题讨论】:

  • 怎么不行?它编译吗?你有什么例外吗?

标签: c# datagridview multithreading


【解决方案1】:

您可以使用以下代码:

   private void GRIDLOG(string ulke, string url, string ip = "")
    {
        object[] myArray = new object[] { ulke, url, ip};
        if (this.InvokeRequired)
            dgLogGrid.Invoke((MethodInvoker)(() => dgLogGrid.Rows.Add(myArray)));
        else dgLogGrid.Rows.Add(myArray);
    }

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      【解决方案3】:

      希望这有帮助 =]

      private object[] DatagridBuffer(Person p)
      {
          object[] buffer = new object[1];
          buffer[0] = p.FirstName;
          buffer[1] = p.LastName;
          return buffer;
      {
      
      public void ListPeople() 
      {
          List<DatagridViewRow> rows = new List<DataGridViewRow>();
          Dictionary<int, Person> list = SqlUtilities.Instance.InstallationList();
          int index = 0;
          foreach (Person p in list.Values) {
              rows.Add(new DataGridViewRow());
              rows[index].CreateCells(datagrid, DatagridBuffer(p));
              index += 1;
          }
          UpdateDatagridView(rows.ToArray());
      }
      
      public delegate void UpdateDatagridViewDelegate(DataGridViewRow[] list);
      public void UpdateDatagridView(DataGridViewRow[] list)
      {
          if (this.InvokeRequired)
          {
              this.BeginInvoke(
                   new UpdateDatagridViewDelegate(UpdateDatagridView), 
                   new object[] { list }
              );
          }
          else
          {
              datagrid.Rows.AddRange(list);
          }
      }
      

      如果您发现我的代码不正确,或者可以改进,请发表评论。

      【讨论】:

        【解决方案4】:

        您需要传递参数数组。 您在拨打this.BeginInvoke时犯了一个错误

        试试这样:

        this.BeginInvoke(new GRIDLOGDelegate(GRIDLOG), new object[] { ulke, url, ip });
        

        其他一切似乎都是正确的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-16
          • 2014-06-30
          • 2021-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多