[RESOLVED] Datagridview and ContextMenuStrip
Hi!
On my Form there is a DataGridView that has a ContextMenuStrip bound to.
ContextMenuStrip has one button "Delete".
When I Right click on DataGridView then it displays the ContextMenuStrip, highlights entire row BUT the little arrow on the rowheader doesn't come along. That means if I click "Delete" button it will delete the row where this arrow is positioned.
Is there a way to get around this problem?
Here is my code for deleting:
Code:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Try
If Not DataGridView.CurrentRow.IsNewRow Then
DataGridView.Rows.Remove(DataGridView.CurrentRow)
Me.Validate()
Me.BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet)
MsgBox("Row deleted successfully!", MsgBoxStyle.Information, "Database")
End If
Catch ex As Exception
MsgBox("Row was not deleted (row not selected)", MsgBoxStyle.Information, "Database")
End Try
End Sub
And here is my DataGridView right click code:
Code:
Private Sub DataGridView_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseDown
If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
DataGridView.ClearSelection()
DataGridView.Rows.Item(e.RowIndex).Selected = True
End If
End Sub
P.S!
Have tried with event cellmouseclick too...doesn't work.
Re: Datagridview and ContextMenuStrip
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
Re: Datagridview and ContextMenuStrip
Quote:
Originally Posted by
dunfiddlin
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
I have tried with different selection modes. They select the row but the arrow on the rowheader remains its position(when I use right click).
With left click the arrow on row header comes along.
Re: Datagridview and ContextMenuStrip
It does? Not on my system it doesn't! Screenshots?
Re: Datagridview and ContextMenuStrip
I have removed the content of this post because the member posting never responded to my suggestion which is inconsiderate.
2 Attachment(s)
Re: Datagridview and ContextMenuStrip
Quote:
Originally Posted by
dunfiddlin
It does? Not on my system it doesn't! Screenshots?
This is when the cell is leftclicked(notice that the little arrow is on the same line as the highlighted row).
Attachment 96369
Now the right click. As you see the cell that I clicked highlights the row, but the little arrow doesn't come along. Which means that if I click delete or any other button that gets the selected row index it will apply to the row where the arrow is, not the higlighted one.
Attachment 96371
My current datagridview allows to add new lines and edit lines...
Re: Datagridview and ContextMenuStrip
A selected row doesn't have to be the current row. Your right click sets the row to selected, but this doesn't automatically makes it the current row. Your delete code trying to delete the current row which may or may not be the selected row...
Re: Datagridview and ContextMenuStrip
Hi again!
Did some rethinking and came up with this solution:
Code:
Private Sub RihmatabelDataGridView_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles RihmatabelDataGridView.CellMouseDown
If e.ColumnIndex >= 0 And e.RowIndex >= 0 And e.Button = Windows.Forms.MouseButtons.Right Then
If RihmatabelDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = False Then
RihmatabelDataGridView.ClearSelection()
RihmatabelDataGridView.CurrentCell = RihmatabelDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
RihmatabelDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected = True
End If
End If
End Sub
It works if users are and aren't allowed to edit or add rows to datagridview.
Thank you for thinking along, appreciate it!
Thread SOLVED!