Populate combobox from excelsheet

Satyapriyanayak
Posted by Satyapriyanayak under VB.NET category on | Points: 40 | Views : 851
We will know how to populate one column from excel to combobox.

Imports System.Data.OleDb
Module Module1
Public con As OleDbConnection
Sub pintu(ByVal s As String)
con = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source='" & s & " '; " & "Extended Properties=Excel 8.0;")

End Sub
Public com As OleDbCommand
Public ds As DataSet
Public oledbda As OleDbDataAdapter
Public dt As DataTable
Public str As String
End Module



Imports System.Data.OleDb
Public Class Form1

Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
Dim openfiledialog1 As New OpenFileDialog
openfiledialog1.ShowDialog()
openfiledialog1.Filter = "allfiles|*.xls"
TextBox1.Text = openfiledialog1.FileName
End Sub

Private Sub btndisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisplay.Click
pintu(TextBox1.Text)
Try
con.Open()
str = "select * from [sheet1$]"
com = New OleDbCommand(str, con)
oledbda = New OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "[sheet1$]")
con.Close()
dt = ds.Tables("[sheet1$]")
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
ComboBox1.Items.Add(dt.Rows(i).ItemArray(1))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Text = "Please select"
End Sub
End Class

Comments or Responses

Login to post response