Results 1 to 8 of 8

Thread: [RESOLVED] Datagridview and ContextMenuStrip

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Resolved [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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Datagridview and ContextMenuStrip

    Quote Originally Posted by dunfiddlin View Post
    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.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Datagridview and ContextMenuStrip

    Quote Originally Posted by dunfiddlin View Post
    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).

    Name:  leftclick.JPG
Views: 2435
Size:  11.1 KB

    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.

    Name:  rightclick.JPG
Views: 2473
Size:  11.2 KB



    My current datagridview allows to add new lines and edit lines...

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    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
  •  



Click Here to Expand Forum to Full Width