Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click DataGridView1.Columns("Column1").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader Try Dim ofd As New OpenFileDialog ofd.Title = "Please select the excel which you want to import" ofd.Filter = "Excel files (*.xls)|*.xlsx" If ofd.ShowDialog = DialogResult.OK Then DataGridView1.Visible = True filename = ofd.FileName Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";" Dim con As OleDbConnection = New OleDbConnection(strin) If con.State = ConnectionState.Closed Then con.Open() Dim command As OleDbCommand = New OleDbCommand() command.CommandText = "Select * from [Sheet1$] " command.Connection = con Dim adapter As OleDbDataAdapter = New OleDbDataAdapter() adapter.SelectCommand = command Dim dt As New DataSet adapter.Fill(dt, "DT") DataGridView1.DataSource = dt.Tables(0) con.Close() Bind(filename) End If Else Exit Sub End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Public Sub Bind(ByVal strExcelFilePath As String) Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";" Dim con As OleDbConnection = New OleDbConnection(strin) If con.State = ConnectionState.Closed Then con.Open() Dim command As OleDbCommand = New OleDbCommand() command.CommandText = "Select Distinct Department from [Sheet1$] " command.Connection = con Dim adapter As OleDbDataAdapter = New OleDbDataAdapter() adapter.SelectCommand = command Dim dt As New DataSet adapter.Fill(dt, "DT") ComboBox1.DisplayMember = dt.Tables(0).Columns(0).ColumnName ComboBox1.DataSource = dt.Tables(0) con.Close() End If End Sub Public Sub ComboBind(ByVal strExcelFilePath As String) Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";" Dim con As OleDbConnection = New OleDbConnection(strin) If con.State = ConnectionState.Closed Then con.Open() Dim command As OleDbCommand = New OleDbCommand() command.CommandText = "Select Distinct Department from [Sheet1$] where Department=" & ComboBox1.Text command.Connection = con Dim adapter As OleDbDataAdapter = New OleDbDataAdapter() adapter.SelectCommand = command Dim dt As New DataSet 'adapter.Fill(dt, "DT") adapter.Fill(dt) dt.Tables(0).Columns(0).ColumnName = ComboBox1.DisplayMember DataGridView1.DataSource = dt.Tables(0) End If End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ' Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";" If ComboBox1.SelectedIndex = 0 Then ComboBind(filename) End If End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)