将DataSet填充到ComboBox中并填充TextBox [英] Populate DataSet into a ComboBox and fill a TextBox
问题描述
我正在尝试从DataSet填充ComboBox,而DataSet的一列也应填充TextBox.
I'm trying to fill a ComboBox from a DataSet while a column of the DataSet should fill a TextBox, too.
示例表:
sid | sname | surl
---------------------------
1 | Google | www.google.com
2 | Bing | www.bing.com
3 | Yahoo | www.yahoo.com
现在我想将 Google
, Bing
等作为ComboBox中的 SelectedText
,而将 SelectedValue
是 1
, 2
等.
当我选择 Google
时,我想将 www.google.com
填充到文本框中.
Now I want Google
, Bing
, etc. as the SelectedText
in the ComboBox, while the SelectedValue
is 1
, 2
, etc.
When I select Google
, I want www.google.com
filled into the TextBox.
代码:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tb As DataTable = Dataset1.Table1
ComboBox1.DataSource = tb
ComboBox1.DisplayMember = "sname"
ComboBox1.ValueMember = "sid"
TextBox1.Text = DataSet1.Table1.FindBysid(ComboBox1.SelectedValue).surl
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Text = DataSet1.Table1.FindBysid(ComboBox1.SelectedValue).surl
End Sub
End Class
Afaik,初始选择应有的效果.
组合框选择 Google
,并将 TextBox1.Text
设置为 www.google.com
.
但随后它会遇到以下错误:
Afaik, the initial selection works as it should.
The ComboBox selects Google
and setss TextBox1.Text
to www.google.com
.
But then it rans on the following error:
System.InvalidCastException:类型为DataRowView的无效转换输入整数.
System.InvalidCastException: "Invalid Conversion of Type DataRowView to Type Integer.
这发生在 SelectedIndexChanged
事件上.
我真的不知道为什么第一个TextBox分配能很好地工作,而第二个TextBox分配却由于转换错误而运行.
This happens on the SelectedIndexChanged
Event.
I really don't know why the first TextBox assignment works pretty well while the second one in the event runs on a convertion error.
有什么建议吗?
推荐答案
示例代码以测试 BindingSource
和 Binding
code> 类.
Sample code to test the functionality of the BindingSource
and Binding
classes.
使用一些字段构建DataTable(如问题中所提供);将DataTable用作 BindingSource
对象的数据源.
Build a DataTable with some fields (as provided in the question); use the DataTable as the DataSource of a BindingSource
object.
使用相同的DataSource(先前定义的 BindingSource
对象)将绑定到TextBox Text
属性.
The BindingSource is then set as the DataSource of a ComboBox (here, ComboBox1
).
The DisplayMember
and ValueMember
are also set to the desired fields (set these properties possibly before assigning the control's DataSource).
Then add a Binding to a TextBox Text
property using the same DataSource (the BindingSource
object previously defined).
当ComboBox SelectedItem
更改(以代码形式或由于用户选择而更改)时, TextBox.Text
属性将相应地更新:
When the ComboBox SelectedItem
changes (in code or because of a user selection), the TextBox.Text
property will be updated accordingly:
Friend bindingSource As BindingSource = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable("TestTable")
dt.Columns.AddRange({
New DataColumn("sid", GetType(Integer)) With {
.AutoIncrement = True, .AutoIncrementStep = 1, .AutoIncrementSeed = 1
},
New DataColumn("sname", GetType(String)),
New DataColumn("surl", GetType(String))
})
dt.Rows.Add(New Object() {Nothing, "Google", "www.google.com"})
dt.Rows.Add(New Object() {Nothing, "Bing", "www.bing.com"})
dt.Rows.Add(New Object() {Nothing, "Yahoo", "www.yahoo.com"})
bindingSource = New BindingSource(dt, "")
ComboBox1.ValueMember = "sid"
ComboBox1.DisplayMember = "sname"
ComboBox1.DataSource = bindingSource
TextBox1.DataBindings.Add(
New Binding("Text", bindingSource, "surl", False, DataSourceUpdateMode.OnPropertyChanged))
ComboBox1.SelectedIndex = 0
End Sub
这篇关于将DataSet填充到ComboBox中并填充TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!