Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add event to delete button in a data grid view... vb.net

Posted on 2014-10-21
7
Medium Priority
?
2,237 Views
Last Modified: 2014-10-21
The DGV I have gets filled with data in run-time. It has a delete buttons (in the row headers) but they don't show up in design view. Therefore, I am not able just double click on it to access its click event.

Question: How can I add a click event to this button?
0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 40395090
Use the cellclick event of the datagridview.  Then you have to compare the column index to make sure the column with the button was the one that was clicked.
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 2000 total points
ID: 40395104
Here's some sample code:
First checking for a valid row, then comparing the clicked index with the index of a column. In this case, the dgv is dgvMyMilestones and the column with the button is dgvMyMilstones_Milestone. I reference the column name so I can rearrange columns without modifying code.
 If e.RowIndex > -1 Then
            Select Case e.ColumnIndex
                Case Me.dgvMyMilestones.Columns(Me.dgvMyMilestones_Milestone.Name).Index
                    Dim dgr As DataGridViewRow = Me.dgvMyMilestones.Rows(e.RowIndex)
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40395117
Thanks for the post. While I am trying your post, please take a look to see if the following is a worablw solution:


    Private Sub OrderDGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles OrderDGV.CellContentClick

        Dim x As String

        x = OrderDGV.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

        If x = "Delete -->" Then
            MessageBox.Show(x.ToString)

        End If

End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40
ID: 40395133
If you mean the selection button, the grey button that shows as a row header, you can react with the following code:

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

	If e.ColumnIndex = -1 Then
		MessageBox.Show("You clicked on the header of row " & e.RowIndex)
	Else
		MessageBox.Show("You clicked in cell (" & e.ColumnIndex & "," & e.RowIndex & ")")
	End If

End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 40395141
cellcontentclick is different than cellclick, it might not make a difference if your column is a button but it seems like cellcontentclick is a little more stingy on when it accepts a click (i.e. only if you click the content in the cell and not blank space in the cell).
Looks like your message box will show "Delete -->" when the clicked cell also shows "Delete -->".
If that's the goal, looks like it will work.
If you define your columns, then you can reference the name of the column.
Name your button column OrderDGV_Delete and then you can compare e.columnindex with OrderDGV.cells(OrderDRV_Delete.name).index
IF e.rowindex >-1 andalso e.columnindex = OrderDGV.cells(OrderDRV_Delete.name).index then...
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40395417
Hi James,

Thank you for the good solution. I did not see your post when I closed the question. Do you want me to post a request to split the point?

Mike
0
 
LVL 40
ID: 40395727
I would appreciate it. Thank you.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question