views:

45

answers:

2

How to add a control in DataGridView? Using Button event. For example i want to create a new a row and column in DataGridView, this i want to happen through button control. How can i do it? I am using C#.net and MS-Access.

+1  A: 

Your question doesn't match it's title. The title asks about controls but the question is about rows and columns, I'm ignoring the title and I'm assuming it's an unbound DataGridView.

This MSDN link shows how to add rows and this shows how to add columns.

ho1
yes of course, thanks a lot ho1, i hope this will help me a lot
sameer
A: 

Here is a piece of code for adding a control into the 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);
   }
sameer