Our Man in Bananas
Our Man in Bananas

Reputation: 5979

.NET Windows Forms Datagridview mousedown event not firing

I have an application written in VS.NET Visual Basic on Visual Studio 2005.

It has a Windows form, with a DataGridView control on it to display data retrieved from a database.

I want to allow the user to right-click on a record, see a context menu and be able to click delete to delete just that record from the database.

Unfortunately I can't even get the mouse down or click events to fire (in debug mode, with breakpoints set)

here is my code:

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    MessageBox.Show("You right-clicked on the grid at", "Unpaid", MessageBoxButtons.OK)
End Sub

Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
    MessageBox.Show("You right-clicked on the grid at", "Unpaid", MessageBoxButtons.OK)
End Sub

Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown

    If e.Button = Windows.Forms.MouseButtons.Right Then
        MessageBox.Show("Clicked right")
    else 
        MessageBox.Show("Clicked left")
    End If

    Dim r As Rectangle
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Button == MouseButtons.Right)
        DataGridView1.Rows(e.RowIndex).Selected = True
        r = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
        contextMnu.Show(DataGridView1, r.Left + e.X, r.Top + e.Y)
    End If
End Sub

Private Sub DeleteChequeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteChequeToolStripMenuItem.Click
    MessageBox.Show("You right-clicked on the grid", "Unpaid", MessageBoxButtons.OK)
End Sub

As you can see, I am trying to get some event, any event to fire, and none of code is running when I right-click or even left -click.

BTW, there is data in the grid at run time, and as far as I am aware, it is enabled

So what am I doing wrong here? Is there some feature I need to add to the DataGridView control?

Upvotes: 0

Views: 3330

Answers (2)

Our Man in Bananas
Our Man in Bananas

Reputation: 5979

very sorry about my dumbness (total beginner on VS, coming from VB6).

It turns out that the VB Project settings had Build only on Compile for release set to True, so even though I thought I was running my changed code, I wasn't!

So after I changed that, and ran Project...Clean followed by Project... Build my events started working.

Thanks anyway! Philip

Upvotes: 0

Patratacus
Patratacus

Reputation: 1811

I was playing with your code and the CellMouseDown works when clicking in a cell somewhere in a populated datagridview. I populated the data manually during the design time. I haven't tried this during the run time mode.

You can get the empty DataGridView1 to fire events MouseDown() and DoubleClick() though even if it's not populated. However, this by itself doesn't know what cells you are clicking. Also, The MouseDown would take precedent over the DoubleClick and you won't see double click here.

 Private Sub DataGridView1_MouseDown(sender As Object, e As MouseEventArgs) Handles     DataGridView1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Right Then

        MessageBox.Show("Clicked right")
    Else
        MessageBox.Show("Clicked left")
    End If
 End Sub

 Private Sub DataGridView1_DoubleClick(sender As Object, e As EventArgs) Handles DataGridView1.DoubleClick
    MessageBox.Show("Double Clicked")
 End Sub

[EDIT 2/25/2013]

I tried to dynamically add data to the datagridview using the following code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim testData As New List(Of String)
    testData.Add("1")
    testData.Add("2")
    testData.Add("3")
    Dim dataBindingSource As New BindingSource
    dataBindingSource.DataSource = testData
    DataGridView1.DataSource = dataBindingSource
End Sub

I still can fire the DataGridView1_CellMouseDown events. Perhaps you could try an empty project with just the datagridview and try it out to see where your problem might have occurred?

Upvotes: 1

Related Questions