Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I signed up to your site to get help with a problem and I am so glad I did. I found the help I needed immediately. Thanks to all who contribute to your site..."

Geography

Where in the world do Tek-Tips members come from?

How to handle Click Even in button on Dynamic DataGrid

kgreer (MIS)
25 Apr 12 9:27
I have dynamically built a datagrid in a TabControl.  I am now adding a button to allow the user to edit the information.  The button shows up just fine, but when I click on the button the Click event is not being called.  Below is my code for adding the button.  

 // The tabpage.
                    TabPage newPage = new TabPage(drCurrent[0].ToString());

                    newPage.Text = drCurrent[0].ToString();

                    DataGridView grid = new DataGridView();
                    grid.AllowUserToAddRows = false;
                    grid.AllowUserToDeleteRows = false;
                    

                    DataGridViewButtonColumn bcol = new DataGridViewButtonColumn();
                    bcol.HeaderText = "Edit";
                    bcol.Text = "Edit";
                    bcol.Name = "btnClickMe";
                    bcol.Width = 50;                    
                    bcol.UseColumnTextForButtonValue = true;
                    
                    grid.Columns.Add(bcol);
                    

                    grid.Dock = DockStyle.Fill;

                    grid.DataSource = table;
                    
                    
                    newPage.Controls.Add(grid);                    

                    // Add to the tab control.
                    tabMain.TabPages.Add(newPage);


What do I need to do to get the click even working?
bakershawnm (Programmer)
14 May 12 11:16
Where do you assign the click event to the button? If you are dynamically adding the button you will need to assign the click event to it the way the designer does it.

this.button1.Click += new System.EventHandler(this.button1_Click);

However since it is a datagrid field you would assign it to the cell content click event of the DGV.

grid.Columns.Add(bcol);

grid.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.grid_CellContentClick);

grid.Dock = DockStyle.Fill;

I did get some unexpected results when testing this though. It would occasionally fire the event when clicking on a random column in the grid. Did not troubleshoot further to figure out why but it did consistently fire the event when clicking the edit button.


kgreer (MIS)
14 May 12 11:34
bakershawm

Thanks for the reply.... I finally did exactly what you said by using the click event.
I am not getting any issues showing up, it is working just fine on my end.

dataGridView1.CellContentClick += new DataGridViewCellEventHandler(this.dataGridView1_CellClick);

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close