Solved

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

Posted on 2014-10-21
7
2,054 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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