所以我要做的就是这个。我目前有一个显示用户名列表的ComboBox。加载表单时,此comboBox是一个下拉列表,分析师可以从列表中选择其名称。我想要做的是使用组合框显示“选择用户名”的默认值,当然,当他们选择未显示的下拉框时。
我基本上可以将其添加为访问数据库中的值,但其中的乐趣在哪里。所以,代码时间:
Private Sub FillCombo()
Dim fillcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Server Location.accdb")
Dim query As String = ("SELECT User_Name, User_ID FROM Analysts")
Dim da As New OleDb.OleDbDataAdapter(query, fillcon)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.ValueMember = "User_Name"
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.SelectedIndex = 0
ComboBox1.SelectedItem = "Select UserID"
TextBox10.DataBindings.Clear()
TextBox10.DataBindings.Add("Text", ds.Tables(0), "User_ID")
End Sub
现在FillCombo()被放置在Form Load中并且工作得非常漂亮,唯一不起作用的是......“Select UserID”不会显示为默认文本。想法?
答案 0 :(得分:1)
您无法将项目添加到数据绑定组合框中,也无需将其添加到数据库中,只需将其添加到数据表中
Dim row = ds.Tables(0).NewRow()
row(0) = "Select UserID"
row(1) = 0
ds.Tables(0).Rows.InsertAt(row, 0)
ComboBox1.SelectedIndex = 0
答案 1 :(得分:0)
不设置selecteditem属性,该项目不在列表中。 将text属性设置为您想要的文本
ComboBox1.Text = "Select UserID"
答案 2 :(得分:0)
不要将组合框绑定到数据源。使用此方法填充组合框。然后,您就可以设置文本属性。
da.Fill(ds)
for each dr as datarow in ds.Tables(0).Rows
ComboBox1.Items.Add(dr("User_Name"))
next
ComboBox1.Text = "Select UserID"