在Datagridview中添加控件

时间:2010-06-03 07:58:33

标签: c# datagridview

如何在DataGridView中添加控件?使用Button事件。例如,我想在DataGridView中创建一个新的行和列,我想通过按钮控件发生。我该怎么做? 我正在使用C#.net和MS-Access。

2 个答案:

答案 0 :(得分:1)

您的问题与其标题不符。标题询问控件,但问题是关于行和列,我忽略了标题,我假设它是一个未绑定的DataGridView

This MSDN链接显示了如何添加行,this显示了如何添加列。

答案 1 :(得分:0)

这是一段用于将控件添加到gridview中的代码。

private void addNewRowButton_Click(object sender,EventArgs e)       {

       this.DataGridViewIssue.Rows.Add();//This line will add a new button contol into the grid 
   }

   private void deleteRowButton_Click(object sender, EventArgs e)
   {
       if (this.DataGridViewIssue.SelectedRows.Count > 0 &&
           this.DataGridViewIssue.SelectedRows[0].Index !=
           this.DataGridViewIssue.Rows.Count - 1)
       {
           this.DataGridViewIssue.Rows.RemoveAt(
               this.DataGridViewIssue.SelectedRows[0].Index);
       }
   }

   private void SetupLayout()
   {
       this.Size = new Size(1055, 800);

       addNewRowButton.Text = "Add Row";
       addNewRowButton.Location = new Point(10, 10);
       addNewRowButton.Click += new EventHandler(addNewRowButton_Click);

       deleteRowButton.Text = "Delete Row";
       deleteRowButton.Location = new Point(100, 10);
       deleteRowButton.Click += new EventHandler(deleteRowButton_Click);

       buttonPanel.Controls.Add(addNewRowButton);
       buttonPanel.Controls.Add(deleteRowButton);
       buttonPanel.Height = 50;
       buttonPanel.Dock = DockStyle.Bottom;

       this.Controls.Add(this.buttonPanel);
   }