×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to handle Click Even in button on Dynamic DataGrid

How to handle Click Even in button on Dynamic DataGrid

How to handle Click Even in button on Dynamic DataGrid

(OP)
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?

RE: How to handle Click Even in button on Dynamic DataGrid

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.


RE: How to handle Click Even in button on Dynamic DataGrid

(OP)
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);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login


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