无法在运行时添加多个DataGridViewButtonColumn

时间:2016-09-01 15:30:33

标签: c# winforms datagridview

我正在从另一个类调用一个函数到一个表单来加载datagridView。当我只添加一个dataGirdViewButtonColumn它的工作,但当我添加两个第二个没有添加。另一个问题,我发现是按钮被添加到空行..

enter image description here

功能

  public void LoadDataGridrcv(DataTable d, DataGridView dg)
    {
        DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
        DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
        if (dg.InvokeRequired)
        {
            dg.BeginInvoke((MethodInvoker)delegate()
            {
                dg.Rows.Clear();
                dg.ColumnCount = 7;
                dg.Columns[0].Name = "Order No.";
                dg.Columns[0].Width = 110;
                dg.Columns[1].Name = "Order Date";
                dg.Columns[1].Width = 100;
                dg.Columns[2].Name = "Excepted rcv date";
                dg.Columns[2].Width = 100;
                dg.Columns[3].Name = "Supplier";
                dg.Columns[3].Width = 150;
                dg.Columns[4].Name = "Total Items";
                dg.Columns[4].Width = 80;
                dg.Columns[5].Name = "Total";
                dg.Columns[5].Width = 80;
                dg.Columns[6].Name = "Status";
                dg.Columns[6].Width = 100;
                dg.Columns.Add(btn);
                btn.HeaderText = "Click to view";
                btn.Text = "View";
                btn.Name = "btn";
                btn.UseColumnTextForButtonValue = true;
                btn1.HeaderText = "Click to recieve";
                btn1.Text = "Recieve";
                btn1.Name = "btn1";
                btn1.UseColumnTextForButtonValue = true;
                });
            foreach (DataRow row in d.Rows)
            {

                if (dg.InvokeRequired)
                {
                    dg.BeginInvoke((MethodInvoker)delegate() { dg.Rows.Add(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString(), row[5].ToString(), row[6].ToString()); });
                }


                Thread.Sleep(100);
            }
        }

    }

用法

        reatail r = new reatail();
        t = new Thread(() => r.LoadDataGridrcv(r.loadAllOrder(), dataGridView1));
        t.Start();

0 个答案:

没有答案