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:
-
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
-
Dim ClientsFile = TextBox1.Text
-
'MsgBox(ClientsFile)
-
Dim con As OleDbConnection
-
Dim da As OleDbDataAdapter
-
Dim ds As New DataSet
-
-
If ClientsFile <> "" Then
-
Try
-
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & ClientsFile & ";Extended Properties=Excel 8.0")
-
con.Open()
-
da = New OleDbDataAdapter("select * from [Sheet1$]", con)
-
da.TableMappings.Add("Table", "SAMPLE")
-
da.Fill(ds)
-
DataGridClient.SortOrder.Ascending()
-
-
DataGridClient.DataSource = ds.Tables(0).DefaultView
-
Catch ex As Exception
-
MsgBox(ex.Message)
-
Finally
-
con.Close()
-
End Try
-
End If
-
End Sub
-
Here is the current code I am using to perform the event capture:
-
Private Sub DataGridClient_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridClient.ColumnHeaderMouseClick
-
Dim clickedColumn As String
-
Dim iRow As Integer = DataGridClient.CurrentRow.Index
-
Dim iCol As Integer = DataGridClient.CurrentCell.ColumnIndex
-
Dim RowName As String = DataGridClient.Columns(iCol).HeaderText
-
-
If e.Button = Windows.Forms.MouseButtons.Right Then
-
Dim hit As DataGridView.HitTestInfo = DataGridClient.HitTest(e.X, e.Y)
-
If hit.Type = DataGridViewHitTestType.ColumnHeader Then
-
'this does not produce anything
-
clickedColumn = DataGridClient.Columns(hit.ColumnX).HeaderText
-
End If
-
End If
-
-
MsgBox(clickedColumn)
-
-
ListBox1.Items.Add(RowName)
-
End Sub
-