在运行时更新下拉按钮值 [英] Update Dropdown button values at runtime

查看:64
本文介绍了在运行时更新下拉按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有一个带有来自excel文件的数据源的下拉框。我用下面的代码填写我的下拉框



Hi i have a dropdown box with a datasource from an excel file.I fill my dropdown box with that code

System.Windows.Forms.Application.EnableVisualStyles()
    ' This call is required by the designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.

    Dim a As New OleDbConnectionStringBuilder With
        {.Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = IO.Path.Combine(My.Settings.CargoData, "CargoData.xlsx")
            }
    a.Add("Extended Properties", "Excel 12.0; HDR=Yes;")
    Dim SheetName_2 As String = "CargoData"
    Dim dt_2 As New DataTable
    Using cn_2 As New OleDbConnection With
        {
            .ConnectionString = a.ConnectionString
        }
        cn_2.Open()
        Dim cmd_2 As OleDbCommand = New OleDbCommand(
            <Text>
                SELECT 

                   Type  
                FROM [<%= SheetName_2 %>$] order by Type
            </Text>.Value,
            cn_2
        )
        Cargodt.Load(cmd_2.ExecuteReader)
    End Using



代码是从我的下拉框中的另一种形式。它在frmMain中。在下拉列表中frmCargo我有这个代码


that code is in another form from my dropdown box.It's in frmMain. In dropdown form frmCargo i have this code

Private Sub frmCargo_Load(sender As Object, e As EventArgs) Handles Me.Load
    drpCargoType.DisplayMember = "Type"
    drpCargoType.DataSource = Cargodt
End Sub
Private Sub txtCargoType_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCargoType.KeyDown
    If e.KeyCode = Keys.Enter Then
        Dim objExcel As New Excel.Application     ' Represents an instance of Excel
        Dim objWorkbook As Excel.Workbook     'Represents a workbook object
        Dim objWorksheet As Excel.Worksheet     'Represents a worksheet object
        objWorkbook = objExcel.Workbooks.Add
        objWorkbook = objExcel.Workbooks.Open("C:\Position Reports SBLK\Cargo Data\CargoData.xlsx")
        objWorksheet = CType(objWorkbook.Worksheets.Item("CargoData"), Excel.Worksheet)
        Dim lastRow As Long
        lastRow = objWorksheet.Range("A" & objExcel.Rows.CountLarge).End(Excel.XlDirection.xlUp).Row + 1
        With objWorksheet
            .Range("A" & lastRow).Value = txtCargoType.Text
        End With
        objWorkbook.Save()
        objWorkbook.Close(False)
        objExcel.Quit()
        txtCargoType.Hide()
        drpCargoType.Show()
    ElseIf e.KeyCode = Keys.Escape Then
        txtCargoType.Hide()
        drpCargoType.Show()
    End If
End Sub



上面的代码显示一个文本框在我的工作表中添加值,效果很好,但我想同时用新添加的值更新Dropbox。请帮助。我也有一个代码,带有一个调用该文本框的按钮,但我觉得它无关紧要


The above code shows a textbox to add value in my worksheet,that works great,but i want at the same time to update the dropbox with the new added value.Please help.Also i have a code with a button that calls that textbox but i think its irrelevant

推荐答案

按类型排序
< / 文字 > .Value,
cn_2

Cargodt.Load(cmd_2.ExecuteReader)
结束 使用
order by Type </Text>.Value, cn_2 ) Cargodt.Load(cmd_2.ExecuteReader) End Using



代码在我的下拉框中的另一种形式。它在frmMain中。在下拉列表中frmCargo我有这个代码


that code is in another form from my dropdown box.It's in frmMain. In dropdown form frmCargo i have this code

Private Sub frmCargo_Load(sender As Object, e As EventArgs) Handles Me.Load
    drpCargoType.DisplayMember = "Type"
    drpCargoType.DataSource = Cargodt
End Sub
Private Sub txtCargoType_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCargoType.KeyDown
    If e.KeyCode = Keys.Enter Then
        Dim objExcel As New Excel.Application     ' Represents an instance of Excel
        Dim objWorkbook As Excel.Workbook     'Represents a workbook object
        Dim objWorksheet As Excel.Worksheet     'Represents a worksheet object
        objWorkbook = objExcel.Workbooks.Add
        objWorkbook = objExcel.Workbooks.Open("C:\Position Reports SBLK\Cargo Data\CargoData.xlsx")
        objWorksheet = CType(objWorkbook.Worksheets.Item("CargoData"), Excel.Worksheet)
        Dim lastRow As Long
        lastRow = objWorksheet.Range("A" & objExcel.Rows.CountLarge).End(Excel.XlDirection.xlUp).Row + 1
        With objWorksheet
            .Range("A" & lastRow).Value = txtCargoType.Text
        End With
        objWorkbook.Save()
        objWorkbook.Close(False)
        objExcel.Quit()
        txtCargoType.Hide()
        drpCargoType.Show()
    ElseIf e.KeyCode = Keys.Escape Then
        txtCargoType.Hide()
        drpCargoType.Show()
    End If
End Sub



上面的代码显示一个文本框在我的工作表中添加值,效果很好,但我想同时用新添加的值更新Dropbox。请帮助。我也有一个代码,带有一个调用该文本框的按钮,但我觉得它无关紧要


The above code shows a textbox to add value in my worksheet,that works great,but i want at the same time to update the dropbox with the new added value.Please help.Also i have a code with a button that calls that textbox but i think its irrelevant


我刚刚创建了一个函数来调用Oledb连接,它像魅力一样工作
I just made a function to recall the Oledb connection and it worked like a charm


这篇关于在运行时更新下拉按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆