[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1357
  • Last Modified:

DataGridView DoubleClick

I have a DataGridView on a form.  When the user double-clicks a line, I want to perform a specific action.  I have this working fine.  

However, I want to allow the user to double-click the border between header columns in order to automatically resize the column.  This does not work as it instead fires the double-click event.

How can I make this work?

For a simple example, open a project and put a DataGridView on a form and then paste in this code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.Columns.Add("Col1", "Column1")
        DataGridView1.Columns.Add("Col2", "Column2")
        DataGridView1.Columns.Add("Col3", "Column3")
        DataGridView1.Rows.Add(New Object() {"A", "B", "C"})
        DataGridView1.Rows.Add(New Object() {"D", "E", "F"})
    End Sub

    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        MessageBox.Show("Do something")
    End Sub
0
chlade
Asked:
chlade
  • 4
  • 3
1 Solution
 
chladeAuthor Commented:
Note:  I tried both the DoubleClick and CellDoubleClick events.  They appear to work the same.
0
 
Bob LearnedCommented:
You need to distinguish clicks by checking where the clicks occur, and not process anything if it is a header cell.

Bob
0
 
chladeAuthor Commented:
But that won't result in the column resizing properly.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Bob LearnedCommented:
Are you married to the idea of double-clicks on normal cells?  Can you add right-click context support?

Bob
0
 
chladeAuthor Commented:
I've got other methods of triggering my routine.  I'd just like it to allow double-click ability if it's not that hard.  It seems like it should be easy to do but apparently not without the column header problem.
0
 
Bob LearnedCommented:
It might be possible, but I don't know how much stretching you have to do to make it fit.

Bob
0
 
chladeAuthor Commented:
Thanks.  I have made it work as such:

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvInvoices.CellDoubleClick

        If e.RowIndex <> -1 Then
            MessageBox.Show("Do something")
        Else
            DataGridView1.AutoResizeColumn(e.ColumnIndex)
        End If

    End Sub    
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now