Results 1 to 7 of 7

Thread: ContextMenuStrip - click

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    367

    ContextMenuStrip - click

    I currently have a DatagridView and when I right click on a cell, i want a menu strip to appear with the option to Insert Row & Delete Row.

    The code below achieves this (display of the menu strip items ,Insert Row & Delete Row)

    however when you click on one of the items, i can not get the handler to work -
    The error being in the handler
    Code:
     Private Sub contextMenuDataGrid_Click(sender As Object, e As EventArgs) Handles contextMenuDataGrid.Click
    saying a handles clause required


    Code:
    ublic Class Form1
        Dim index As Integer = 0
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DbTestDataSet.Staff' table. You can move, or remove it, as needed.
            Me.StaffTableAdapter.Fill(Me.DbTestDataSet.Staff)
    
        End Sub
    
        
        Private Sub DataGridView1_CellMouseDown(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
            If e.Button = MouseButtons.Right Then
                DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex)
                Dim contextMenuDataGrid As New ContextMenuStrip
                contextMenuDataGrid.Items.Add("Insert Row")
                contextMenuDataGrid.Items.Add("Delete Row")
                contextMenuDataGrid.Show(DataGridView1, e.Location)
                ' MsgBox("Hello")
            End If
        End Sub
    
        Private Sub contextMenuDataGrid_Click(sender As Object, e As EventArgs) Handles contextMenuDataGrid.Click                   -<------------ERROR ContextMenuDataGrid.Click - 
            MsgBox("QQ")
            If Not DataGridView1.Rows(index).IsNewRow Then 'CHECK IF NEW ROW
                DataGridView1.Rows.RemoveAt(index)
            End If

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,762

    Re: ContextMenuStrip - click

    My guess is because your creating this contextmenustrip at runtime then you need to add the AddHandler for the click event. But why keep creating it every time? Create it in the designer.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: ContextMenuStrip - click

    In order to use something in a Handles clause, it must be a field declared WithEvents. You're signing your grid to a local variable, not such a field. If you created the grid in the designer then it would generate that field automatically. If you're not going to do that for some reason, either declare the field yourself or don't use a Handles clause.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    367

    Re: ContextMenuStrip - click

    Ok after some reading and trial and error, i have the following code which appears to work. The datagridview is is displayed, with the contents bound to a data source.

    I am unsure what the following does ?
    Code:
    ........ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs........
    but it seems to work ok

    you can click delete & it will remove the selected record form the datagrid
    however when i click insert, i get an error
    System.InvalidOperationException: 'Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.'


    Code:
    Public Class Form1
        Private index As Integer = 0
        Private WithEvents contextMenuDataGrid As New ContextMenuStrip
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DbTestDataSet.Staff' table. You can move, or remove it, as needed.
            Me.taStaff.Fill(Me.DbTestDataSet.Staff)
    
        End Sub
    
        ' Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.cl
    
        Private Sub DataGridView1_CellMouseDown(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
            If e.Button = MouseButtons.Right Then
                DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex)
                If contextMenuDataGrid.Items.Count = 0 Then
                    contextMenuDataGrid.Items.Add("Insert Row")
                    contextMenuDataGrid.Items.Add("Delete Row")
                    contextMenuDataGrid.Items.Add("Copy")
                    contextMenuDataGrid.Items.Add("Paste")
                End If
                contextMenuDataGrid.Show(DataGridView1, e.Location)
                ' MsgBox("Hello")
            End If
        End Sub
    
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
            bsStaff.EndEdit()
            taStaff.Update(bsStaff.DataSource)
        End Sub
    
        Private Sub contextMenuDataGrid_Click(sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles contextMenuDataGrid.ItemClicked
            contextMenuDataGrid.Hide()
            '  MsgBox(e.ClickedItem.Text)
            Select Case e.ClickedItem.Text
                Case "Copy"
                Case "Paste"
                Case "Insert Row"
                    DataGridView1.Rows.Insert(index)
                Case "Delete Row"
                    DataGridView1.Rows.RemoveAt(index)
            End Select
    
    
            'If Not DataGridView1.Rows(index).IsNewRow Then 'CHECK IF NEW ROW
            '    '  DataGridView1.Rows.RemoveAt(index)
            'End If
        End Sub
    
    End Class

    Why does the delete work ok for the bound object, but the insert fail and how can i fix it ?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,254

    Re: ContextMenuStrip - click

    Try this...

    Code:
    DataGridView1.Rows.Insert(index, New DataGridViewRow)

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,254

    Re: ContextMenuStrip - click

    Actually, i just read your comment

    Code:
    Dim r As DataRow = Me.DbTestDataSet.Staff.NewRow
    Me.DbTestDataSet.Staff.Rows.InsertAt(r, index)

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,634

    Re: ContextMenuStrip - click

    The delete doesn't work OK. You're removing the row from the grid but you're not deleting the data. Bind your data to the grid via a BindingSource, then use that to add and remove data. RemoveCurrent will actually delete the current row, which means flagging it as Deleted in the underlying DataTable. AddNew will create a new row that you can first populate and then add by calling EndEdit. That new row is returned as an Object reference to a DataRowView. You can cast it as that type, then either populate it directly or cast its Row property as your specific DataRow type and then populate that via its properties.

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