-
Aug 27th, 2012, 01:13 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Delete DataGridView row after menu click
I have a DataGridView that gets populated from another action. I now want to add functionality where the user right clicks on a select row and a menu appears. If they click on the menu item 'Delete', to delete the row they right click on. I am not sure I know how to get a handle on this row and delete.
What do I do in DeleteRow to delete the specific row clicking on?
Code:
....
Dim contextMenuForColumn1 As ContextMenu = New ContextMenu()
...
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Add all default values of controls
contextMenuForColumn1.MenuItems.Add("Delete", AddressOf Me.DeleteRow)
'... other menu items
End Sub
...
Sub DeleteRow()
Dim iRow As Integer = 0
While iRow <= dgvItems.Rows.Count - 1
If dgvItems.Rows(iRow).Cells(0).Selected = True Then
MessageBox.Show(dgvItems.Rows(iRow).Cells(0).Value)
End If
iRow += 1
End While
End Sub
Private Sub dgvItems_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgvItems.MouseUp
Dim hitTestInfo As DataGridView.HitTestInfo
If e.Button = Windows.Forms.MouseButtons.Right Then
hitTestInfo = dgvItems.HitTest(e.X, e.Y)
If hitTestInfo.Type = DataGridViewHitTestType.Cell Or hitTestInfo.ColumnIndex = 0 Then
contextMenuForColumn1.Show(dgvItems, New Point(e.X, e.Y))
End If
End If
End Sub
Last edited by lleemon; Aug 28th, 2012 at 07:53 AM.
Reason: Resolved
-
Aug 27th, 2012, 01:20 PM
#2
Re: Delete DataGridView row after menu click
In DeleteRow() why wouldn't this work?
Code:
Dim dgvRow As DataGridViewRow = DataGridView1.CurrentCell.OwningRow
DataGridView1.Rows.Remove(dgvRow)
or am I missing the question?
-
Aug 27th, 2012, 01:25 PM
#3
Thread Starter
Fanatic Member
Re: Delete DataGridView row after menu click
Yes, this will work if I first click on the row before right click. So I guess I need to first select row in the _MouseUp function before menu is shown.
-
Aug 27th, 2012, 03:46 PM
#4
Thread Starter
Fanatic Member
Re: Delete DataGridView row after menu click
I can't seem to get it where it deletes the row I right click and select delete. The following works only once I click the first cell of the row I want to remove and then right click. Would be nice if I didn't have to select but just happened.
Code:
Dim contextMenuForColumn1 As ContextMenu = New ContextMenu()
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Add all default values of controls
contextMenuForColumn1.MenuItems.Add("Delete", AddressOf Me.DeleteRow)
'... other menu items
End Sub
Sub DeleteRow()
Dim dgvRow As DataGridViewRow = dgvItems.CurrentCell.OwningRow
dgvItems.Rows.Remove(dgvRow)
End Sub
Private Sub dgvItems_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgvItems.MouseDown
Dim hitTestInfo As DataGridView.HitTestInfo
If e.Button = Windows.Forms.MouseButtons.Right Then
hitTestInfo = dgvItems.HitTest(e.X, e.Y)
If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
If Not dgvItems.Rows(hitTestInfo.RowIndex).Selected Then
dgvItems.ClearSelection()
'dgvItems.Rows(hitTestInfo.RowIndex).Selected = True
dgvItems.Rows(hitTestInfo.RowIndex).Cells(hitTestInfo.ColumnIndex).Selected = True
End If
End If
End If
End Sub
Private Sub dgvItems_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgvItems.MouseUp
Dim hitTestInfo As DataGridView.HitTestInfo
If e.Button = Windows.Forms.MouseButtons.Right Then
hitTestInfo = dgvItems.HitTest(e.X, e.Y)
'If column = first column
If hitTestInfo.Type = DataGridViewHitTestType.Cell Or hitTestInfo.ColumnIndex = 0 Then
contextMenuForColumn1.Show(dgvItems, New Point(e.X, e.Y))
End If
End If
End Sub
-
Aug 27th, 2012, 04:51 PM
#5
Re: Delete DataGridView row after menu click
Originally Posted by lleemon
I can't seem to get it where it deletes the row I right click and select delete. The following works only once I click the first cell of the row I want to remove and then right click. Would be nice if I didn't have to select but just happened.
The problem is one of precedence. If you raise a menu from the right click and choose an item then the click location information is lost as the new click on the delete item is registered. One possible solution is to make the right click select the row before the menu opens, eg.
vb.net Code:
Private Sub DataGridView1_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.Button = MouseButtons.Right Then
DataGridView1.Rows(e.RowIndex).Selected = True
'menu code goes in here
End If
End Sub
-
Aug 27th, 2012, 08:16 PM
#6
Thread Starter
Fanatic Member
Re: Delete DataGridView row after menu click
Well, this does work but two things:
1) The menu always appears at one position and not by the cell I right click on.
2) It still is removing the first entry in the datagridview no matter what row I right click on.
Anyone have a working example I can follow?
Code:
Dim contextMenuForColumn1 As ContextMenu = New ContextMenu()
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Add all default values of control
contextMenuForColumn1.MenuItems.Add("Delete", AddressOf Me.DeleteRow)
'... other menu items
End Sub
Sub DeleteRow()
Dim dgvRow As DataGridViewRow = dgvItems.CurrentCell.OwningRow
dgvItems.Rows.Remove(dgvRow)
End Sub
Private Sub dgvItems_CellMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvItems.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
dgvItems.Rows(e.RowIndex).Selected = True
contextMenuForColumn1.Show(dgvItems, New Point(e.X, e.Y))
End If
End Sub
-
Aug 27th, 2012, 09:47 PM
#7
Thread Starter
Fanatic Member
Re: Delete DataGridView row after menu click
Ok, here is another way but totally doesn't work. The menu is always at the top and not right where the cell you right click is. Other then that works fine. Anyone know how in this code I can get it to position the menu properly?
Step 1: Add ContextMenuStrip named ContextMenuStrip1 and Property Items create 'DeleteRow1' ToolStripMenuItem.
Code:
Dim iDGVrow2Del As Integer = 0
Private Sub dgvItems_CellMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvItems.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim hitTestInfo As DataGridView.HitTestInfo
hitTestInfo = dgvItems.HitTest(e.X, e.Y)
Me.dgvItems.Rows(e.RowIndex).Selected = True
Me.iDGVrow2Del = e.RowIndex
Me.dgvItems.CurrentCell = Me.dgvItems.Rows(e.RowIndex).Cells(1)
'Me.ContextMenuStrip1.Show(Me.dgvItems, e.Location)
ContextMenuStrip1.Show(dgvItems, New Point(e.X, e.Y))
End If
End Sub
Private Sub DeleteRow1_Click(sender As System.Object, e As System.EventArgs) Handles DeleteRow1.Click
If Not Me.dgvItems.Rows(Me.iDGVrow2Del).IsNewRow Then
Me.dgvItems.Rows.RemoveAt(Me.iDGVrow2Del)
End If
End Sub
-
Aug 28th, 2012, 06:21 AM
#8
Re: Delete DataGridView row after menu click
The coordinates you are using (e.Location, e.X e.Y) to offset the menu from the top left of the datagridview, are actually the coordinates of the mouse relative to the top left of the cell you clicked in. As is usual, the top left coordinate is deemed to be (0,0), so e.X will never be greater than the cell's width and e.Y will never be greater than the cell's height. The menu will always open in the top left of the datagridview (within the area that a cell would occupy if you could place one up there).
The offset you need to specify is the offset of the mouse pointer relative to the top left of the datagridview. You could do this:
Code:
Me.ContextMenuStrip1.Show(Me.dgvItems, Me.dgvItems.PointToClient(Cursor.Position))
Or use the overload of the .Show method that uses the offset relative to the Screen's origin:
Code:
Me.ContextMenuStrip1.Show(Cursor.Position)
-
Aug 28th, 2012, 07:52 AM
#9
Thread Starter
Fanatic Member
Re: Delete DataGridView row after menu click
Inferrd - Perfect! Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|