Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-21
7
Medium Priority
?
2,336 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
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

963 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