Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

May i ask how to clear the combo box extra empty place

$
0
0

I develop a program is read excel file, i using combo box to let the user select the which sheet file user would like to read. below is my code may i ask how can i delete the extra empty place on my combo box.    

   Private path As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If path = "" Then
            path = OpenFile()
            If path <> "" Then
                Me.Refresh()
                AddComboboxValue(path)
            End If
        Else
            dgv1.DataSource = Nothing
            path = OpenFile()
            AddComboboxValue(path)
        End If
    End Sub

Private Sub AddComboboxValue(ByVal path As String)
        Try
            Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
            Dim excelBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Open(path)
            Dim excelSheets As String() = New String(excelBook.Worksheets.Count - 1) {}
            Dim i As Integer = 0

            For Each wSheet As Microsoft.Office.Interop.Excel.Worksheet In excelBook.Worksheets
                excelSheets(i) = wSheet.Name
                i += 1
            Next
            ComboBox1.Items.Clear()
            ComboBox1.Items.AddRange(excelSheets)
            ComboBox1.SelectedIndex = 0
        Catch e1 As Exception
            ComboBox1.Text = ""
            MessageBox.Show("Read Failed, read the correct file!" & Environment.NewLine & "Error Message:" & Environment.NewLine & e1.Message)
        End Try
    End Sub   

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim ds = New DataSet()
        Dim con As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & path & ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;';"

        Using connection As OleDbConnection = New OleDbConnection(con)
            connection.Open()
            Dim cmd As OleDbCommand = New OleDbCommand()
            Dim oleda As OleDbDataAdapter = New OleDbDataAdapter()
            Dim dt As DataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            Dim sheetName As String = String.Empty

            If dt IsNot Nothing Then
                dt = (From dataRow In dt.AsEnumerable() Where Not dataRow("TABLE_NAME").ToString().Contains("FilterDatabase") Select dataRow).CopyToDataTable()
                sheetName = dt.Rows(ComboBox1.SelectedIndex)("TABLE_NAME").ToString()
            End If

            cmd.Connection = connection
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "SELECT * FROM [" & sheetName & "]"
            oleda = New OleDbDataAdapter(cmd)
            oleda.Fill(ds, "excelData")
        End Using

        dgv1.DataSource = ds.Tables("excelData")
    End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        dgv1.DataSource = Nothing
        dgv1.Rows.Clear()
        ComboBox1.Text = ""
        Me.ComboBox1.Items.Clear()

    End Sub

when user open the file user can select which file user would like to read

when user clear the combo box item, combo box still maintain empty place

may i ask how can i delete combo box extra empty place.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>