Solved

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

Posted on 2014-10-21
7
1,939 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 500 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 33

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now