如何在绑定之前设置组合框的选定值 [英] How to set selected value of combobox before bind
本文介绍了如何在绑定之前设置组合框的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Public Sub Bind(ByVal strExcelFilePath As String)
' ComboBox1.Items.Insert(0, New ListViewItem("All", 0))
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)
ComboBox1.Items.Insert(0, ("All"))
End If
推荐答案
command.Connection = con
Dim adapter As OleDbDataAdapter = 新 OleDbDataAdapter()
adapter.Select Command = command
Dim dt As New DataSet
adapter.Fill(dt, DT)
ComboBox1.DisplayMember = dt.Tables( 0 )。列( 0 )。ColumnName
ComboBox1 .DataSource = dt.Tables( 0 )
ComboBox1.Items.Insert( 0 ,( 所有))
结束 如果
" 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) ComboBox1.Items.Insert(0, ("All")) End If
假设您要预先选择值为我的选择的第3项:(在绑定后执行此操作)
Assuming you are to pre-select 3rd item with value as "my choice": (do this after the binding)
ComboBox1.SelectedIndex = 2
或
or
ComboBox1.SelectedValue = "my choice"
这篇关于如何在绑定之前设置组合框的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文