Connecting Tech Pros Worldwide Forums | Help | Site Map

VB.NET DataGridView ColumnHeader Click

Newbie
 
Join Date: Oct 2007
Posts: 1
#1: Oct 11 '07
I am reading an excel file into a datagridview and trying to perform some function based on the column header that is clicked. I currently can produce an event to occur (msgbox) but, I need to get the current columnheader clicked, not the current column header that is selected. IF anyone has any knowledge of this please help.

This is the loading of excel file:

Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  2.         Dim ClientsFile = TextBox1.Text
  3.         'MsgBox(ClientsFile)
  4.         Dim con As OleDbConnection
  5.         Dim da As OleDbDataAdapter
  6.         Dim ds As New DataSet
  7.  
  8.         If ClientsFile <> "" Then
  9.             Try
  10.                 con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & ClientsFile & ";Extended Properties=Excel 8.0")
  11.                 con.Open()
  12.                 da = New OleDbDataAdapter("select * from [Sheet1$]", con)
  13.                 da.TableMappings.Add("Table", "SAMPLE")
  14.                 da.Fill(ds)
  15.                 DataGridClient.SortOrder.Ascending()
  16.  
  17.                 DataGridClient.DataSource = ds.Tables(0).DefaultView
  18.             Catch ex As Exception
  19.                 MsgBox(ex.Message)
  20.             Finally
  21.                 con.Close()
  22.             End Try
  23.         End If
  24.     End Sub
  25.  
Here is the current code I am using to perform the event capture:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub DataGridClient_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridClient.ColumnHeaderMouseClick
  2.         Dim clickedColumn As String
  3.         Dim iRow As Integer = DataGridClient.CurrentRow.Index
  4.         Dim iCol As Integer = DataGridClient.CurrentCell.ColumnIndex
  5.         Dim RowName As String = DataGridClient.Columns(iCol).HeaderText
  6.  
  7.         If e.Button = Windows.Forms.MouseButtons.Right Then
  8.             Dim hit As DataGridView.HitTestInfo = DataGridClient.HitTest(e.X, e.Y)
  9.             If hit.Type = DataGridViewHitTestType.ColumnHeader Then
  10.                 'this does not produce anything
  11.                 clickedColumn = DataGridClient.Columns(hit.ColumnX).HeaderText
  12.             End If
  13.         End If
  14.  
  15.         MsgBox(clickedColumn)
  16.  
  17.         ListBox1.Items.Add(RowName)
  18.     End Sub
  19.  
Reply


Similar .NET Framework bytes