Solved

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

Posted on 2014-10-21
7
2,153 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 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

624 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