0

有一个后台工作人员,一个 tableLayoutPanel,以及我添加(动态 - 以编程方式)到 tablelayoutpanel 的一堆网格视图和标签。

上面的代码工作正常,但我需要在每次将添加到 gridview 的行上添加一个 onclick 事件。我怎样才能做到这一点?

 var lbltotalcount = tableLayoutPanel1.Controls.Find("lbltotalcount_" + GridId, true);
            ((Label)lbltotalcount[0]).Invoke((MethodInvoker)delegate
            {
                ((Label)lbltotalcount[0]).Text = (Convert.ToInt32(((Label)lbltotalcount[0]).Text) + NumOfMatch).ToString();
            });    

            var obj = tableLayoutPanel1.Controls.Find("dgv_" + GridId, true);


            ((DataGridView)obj[0]).Invoke((MethodInvoker)delegate
            {
                string[] row = new string[] { Word, txturl.Text, Url, NumOfMatch.ToString() };

                DataGridViewRow dgvRow = new DataGridViewRow();
                DataGridViewCell dgvCell;

                dgvCell = new DataGridViewTextBoxCell();                
                dgvCell.Value = Word;
                dgvRow.Cells.Add(dgvCell);                

                dgvCell = new DataGridViewTextBoxCell();
                dgvCell.Value = txturl.Text;
                dgvRow.Cells.Add(dgvCell);

                dgvCell = new DataGridViewTextBoxCell();
                dgvCell.Value = Url;
                dgvRow.Cells.Add(dgvCell);

                dgvCell = new DataGridViewTextBoxCell();
                dgvCell.Value = NumOfMatch.ToString();
                dgvRow.Cells.Add(dgvCell);                                              

                ((DataGridView)obj[0]).Rows.Add(dgvRow);
                ((DataGridView)obj[0]).Refresh();
                ((DataGridView)obj[0]).Update();
            });                    
4

1 回答 1

2

我不确定那是你想要的..让我们试试吧..

  void yourMethod()
 {
  var lbltotalcount = tableLayoutPanel1.Controls.Find("lbltotalcount_" + GridId, true);
          ((Label)lbltotalcount[0]).Invoke((MethodInvoker)delegate
        {
            ((Label)lbltotalcount[0]).Text =     (Convert.ToInt32(((Label)lbltotalcount[0]).Text) + NumOfMatch).ToString();
        });    

        var obj = tableLayoutPanel1.Controls.Find("dgv_" + GridId, true);

       ((DataGridView)obj[0]).SelectionChanged += new EventHandler(My_SelectionChanged);


        ((DataGridView)obj[0]).Invoke((MethodInvoker)delegate
        {
            string[] row = new string[] { Word, txturl.Text, Url, NumOfMatch.ToString() };

            DataGridViewRow dgvRow = new DataGridViewRow();
            DataGridViewCell dgvCell;

            dgvCell = new DataGridViewTextBoxCell();                
            dgvCell.Value = Word;
            dgvRow.Cells.Add(dgvCell);                

            dgvCell = new DataGridViewTextBoxCell();
            dgvCell.Value = txturl.Text;
            dgvRow.Cells.Add(dgvCell);

            dgvCell = new DataGridViewTextBoxCell();
            dgvCell.Value = Url;
            dgvRow.Cells.Add(dgvCell);

            dgvCell = new DataGridViewTextBoxCell();
            dgvCell.Value = NumOfMatch.ToString();
            dgvRow.Cells.Add(dgvCell);                                              

            ((DataGridView)obj[0]).Rows.Add(dgvRow);
            ((DataGridView)obj[0]).Refresh();
            ((DataGridView)obj[0]).Update();
          var index = ((DataGridView) obj[0]).RowCount;
          ((DataGridView) obj[0]).SelectedRows[index - 1].Selected = true;
        });  
  }

   void My_SelectionChanged(object sender, EventArgs e)
    {
      // bla bla ...

    }
于 2012-07-01T02:03:14.907 回答