Results 1 to 3 of 3

Thread: Binding ComboBox to data from Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    12

    Binding ComboBox to data from Excel

    Hi Everyone,

    I have bound data from Excel to my project, but now I am trying to pull a specific column from the table and put the information in a combobox. I am trying to pull dates from column O of my excel file, and I want the dates to go into the combobox with no repeats. Does anyone have any suggestions?

    Code:
    Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
        "data source='" & strFileName & "';" & _
        "Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
            'moApp.Visible = True
    
            Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
                MyConnection.Open()
    
                Dim cmd As OleDbCommand = New OleDbCommand( _
                "SELECT * FROM [Sheet1$O]", MyConnection)
                'Throws an error that [Sheet1$O] is not valid
                Dim dr As System.Data.IDataReader = cmd.ExecuteReader
                Dim dt As New DataTable
    
                While dr.Read()
                    dateComboBox.Items.Add(dr.Item("*").ToString)
                End While
    
            End Using

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Binding ComboBox to data from Excel

    try this:

    vb Code:
    1. Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
    2.     "data source='" & strFileName & "';" & _
    3.     "Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
    4. 'moApp.Visible = True
    5.  
    6. Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
    7.     MyConnection.Open()
    8.  
    9.     Dim da As New OleDbDataAdapter( _
    10.     "SELECT * FROM [Sheet1$O]", MyConnection)
    11.    
    12.     Dim dt As New DataTable
    13.     da.Fill(dt)
    14.  
    15.     dateComboBox.displaymember = dt.Columns(0).ColumnName
    16.     dateComboBox.datasource = dt
    17.        
    18. End Using

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    12

    Using ComboBox as a filter

    Hi Paul,

    Your code worked for me! Thanks so much! Now I was wondering if you could help me with another problem. I am pulling different information from the same file later, and I need to use the combobox as a filter for the other information. I already have a filter in place, and I was wondering if I could add to that filter or if I have to make another filter? Let me know if any of this was confusing!


    This is the code for binding the combobox to the information
    Code:
            Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
                "data source='" & strFileName & "';" & _
                "Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
            'moApp.Visible = True
    
            Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
                MyConnection.Open()
    
                Dim da As New OleDbDataAdapter( _
                "SELECT DISTINCT * FROM [Sheet1$O5:O]", MyConnection)
    
                Dim dt As New DataTable
                da.Fill(dt)
    
                dateComboBox.DisplayMember = dt.Columns(0).ColumnName
                dateComboBox.DataSource = dt
                ediDate = dateComboBox.SelectedItem.ToString()
            End Using


    Here is the code for the 2nd information pull where I need the filter
    Code:
    Private Sub dataButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dataButton.Click
            Dim cnRange As String = "provider=Microsoft.Jet.OLEDB.4.0; " & _
        "data source='" & strFileName & "';" & _
        "Extended Properties=""Excel 8.0; IMEX=1; HDR=No;"""
            'moApp.Visible = True
    
            Using MyConnection As New System.Data.OleDb.OleDbConnection(cnRange)
                MyConnection.Open()
    
                Dim cmd As OleDbCommand = New OleDbCommand( _
                "SELECT * FROM [Sheet1$J4:O]", MyConnection)
                Dim dr As System.Data.IDataReader = cmd.ExecuteReader
                Dim dt As New DataTable
                dt.Load(dr)
                DataGridView1.DataSource = dt
    
                Me.BindingSource1.DataSource = dt
                Me.BindingSource1.Filter = String.Format("F1 = '76812SNE      
                    A010M2'".ToString)
                Chart1.DataSource = BindingSource1
    
                Chart1.Series("Series1").XValueMember = "F3"
                Chart1.Series("Series1").YValueMembers = "F4"
    
                Chart1.DataBind()
            End Using
        End Sub
    Thanks for your help!
    Last edited by edolf8; Apr 28th, 2011 at 12:41 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width