Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problem with Datagridview ColumnHeaderMouseClick
 

Problem with Datagridview ColumnHeaderMouseClick

I am trying to retrieve the column name that is relative to the column header that is clicked.  My code is

 

Private Sub grd_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grd.ColumnHeaderMouseClick

 

Dim hit As DataGridView.HitTestInfo = grd.HitTest(e.X, e.Y)

 

If hit.Type = DataGridViewHitTestType.ColumnHeader Then

sColumnHeaderClicked = grd.Columns(hit.ColumnX).Name

 

End If

 

End Sub

The problem is that the values of the hit.ColumnX and hit.ColumnIndex never change.   I would have thought that these would change depending on which header was clicked.

Can anyone help or enlighten me on this?

TIA

Deucy  Saturday, October 03, 2009 12:57 PM

You dont need to use HitTestInfo.

Just use

dataGridView1.Columns(e.ColumnIndex).Name

  • Proposed As Answer byTamer OzMVPSaturday, October 03, 2009 6:42 PM
  • Marked As Answer byDeucy Sunday, October 04, 2009 7:31 PM
  •  
Tamer Oz  Saturday, October 03, 2009 5:34 PM

You dont need to use HitTestInfo.

Just use

dataGridView1.Columns(e.ColumnIndex).Name

  • Proposed As Answer byTamer OzMVPSaturday, October 03, 2009 6:42 PM
  • Marked As Answer byDeucy Sunday, October 04, 2009 7:31 PM
  •  
Tamer Oz  Saturday, October 03, 2009 5:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Multiple comboboxes binding and selection issue
• About the error "operation not valid due to the current state of the object"
• Datagridview - ComboBox column getting blanked out
• beginner problem with databinding
• Resolve duplicate/multiple entries in in Combobox (C# 2005)
• Yet another Data binding question, my control doesnt update the datasource.
• problem with BindingSource.moveNext
• Bind custom CheckedListBox Control to single field in DB
• Is that modifier problem?
• Bind data source to DataGridView RowHeader value