|
-
Feb 11th, 2013, 02:38 PM
#1
Thread Starter
Lively Member
[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.
-
Feb 11th, 2013, 02:42 PM
#2
Re: Datagridview and ContextMenuStrip
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 11th, 2013, 03:08 PM
#3
Thread Starter
Lively Member
Re: Datagridview and ContextMenuStrip
 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.
-
Feb 11th, 2013, 03:36 PM
#4
Re: Datagridview and ContextMenuStrip
It does? Not on my system it doesn't! Screenshots?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 12th, 2013, 12:07 AM
#5
Re: Datagridview and ContextMenuStrip
I have removed the content of this post because the member posting never responded to my suggestion which is inconsiderate.
Last edited by kareninstructor; Feb 12th, 2013 at 02:44 PM.
-
Feb 12th, 2013, 02:20 AM
#6
Thread Starter
Lively Member
Re: Datagridview and ContextMenuStrip
 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).

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.

My current datagridview allows to add new lines and edit lines...
-
Feb 12th, 2013, 11:33 AM
#7
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...
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 12th, 2013, 02:31 PM
#8
Thread Starter
Lively Member
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!
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
|