vb.net 如何在数据绑定组合框上选择值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5338104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select value on a databound combobox?
提问by Zolomon
I have a ComboBox
that's been bound to a DataView
that's taken a filled DataTable
. This DataTable
has three columns. Before the DataView
is bound to the ComboBox
I add another column and set some values.
我有一个ComboBox
被绑定到一个DataView
被填充的DataTable
. 这DataTable
有三列。在DataView
绑定到ComboBox
我之前添加另一列并设置一些值。
Dim table As DataTable = _retrieve.GetAllVersionsTable()
table.Columns.Add("FirstRow", GetType(Boolean))
Dim row As DataRow = table.NewRow()
row("ID") = -1
row("SomeValue") = -1
row("SomeText") = "N/A"
row("FirstRow") = True 'Fort sorting...
Dim view As DataView = New DataView(table)
view.Sort = "FirstRow DESC, SomeText DESC"
table.Rows.InsertAt(row, 0)
comboBox.DataSource = view
comboBox.ValueMember = "ID"
comboBox.DisplayMember = "SomeText"
Later on, I retrieve the same data and create a new DataTable
and bind it to a DataGridView
in another form. From this form I set a value for the identity column from selected row in the DataGridView
.
稍后,我检索相同的数据并创建一个新数据并将其DataTable
绑定到DataGridView
另一种形式的 a。在这个表单中,我为DataGridView
.
When I get back to the first form, with the ComboBox
, I wish to select the ComboBox
row that has the same value, that I set from the second form, tied to its ValueMember
property. I thought that SelectedValue
would work for this but it doesn't. What else can I do?
当我回到第一个表单时ComboBox
,我希望选择ComboBox
具有相同值的行,该行是我从第二个表单设置的,并绑定到它的ValueMember
属性。我认为这SelectedValue
可以解决这个问题,但事实并非如此。我还可以做些什么?
comboBox.SelectedValue = myIdentityValue
Since the rows are different I can't use SelectedItem
either. Is there a way I can select the appropriate row without having to loop over all existing rows?
由于行不同,我也不能使用SelectedItem
。有没有一种方法可以选择适当的行而不必遍历所有现有行?
采纳答案by Meta-Knight
According to MSDN:
根据 MSDN:
SelectedValue: Gets or sets the value of the member property specified by the ValueMember property.
SelectedValue:获取或设置由 ValueMember 属性指定的成员属性的值。
So according to documentation, doing something like this should work, assuming 123 is an ID present in the combobox, because ID has been set as the ValueMember:
所以根据文档,做这样的事情应该可以工作,假设 123 是组合框中存在的 ID,因为 ID 已设置为 ValueMember:
comboBox.SelectedValue = 123
If you can't get it to work (maybe binding with DataView doesn't work well?), then you could use SelectedIndex which always works.
如果你不能让它工作(也许与 DataView 绑定不能很好地工作?),那么你可以使用始终有效的 SelectedIndex 。
回答by Delcon
For anybody that comes across this topic, has the same problems like Zolom or me. I think I found a important thing that is not documented:
对于遇到此主题的任何人,都会遇到与 Zolom 或我一样的问题。我想我发现了一个没有记录的重要事情:
You must pass the same datatype to .SelectedValue. If not the search will fail without any hint.
您必须将相同的数据类型传递给 .SelectedValue。如果不是,搜索将在没有任何提示的情况下失败。
Let's say you fill your DataSource with with a database and have lets say integer as value-field you have to pass an integer to the SelectedIndex. If it was string you pass a string.
假设你用一个数据库填充你的数据源,并让整数作为值字段,你必须将一个整数传递给 SelectedIndex。如果它是字符串,则传递一个字符串。
I hope this helps...
我希望这有帮助...
回答by Fernando Rios
That issue you can solve setting to Nothingthe property datasource before than set the datasource:
在设置数据源之前,您可以解决将属性数据源设置为Nothing的问题:
Combobox.datasource=nothing
Combobox.DisplayMember="Data";
Combobox.ValueMember="ID";
Combobox.datasource = NewDataTable;
Combobox.SelectedValue = 4;
回答by shong
Use Combobox.text=""
. It will automatically change the selectedvalue property to your your entered text.
使用Combobox.text=""
. 它会自动将 selectedvalue 属性更改为您输入的文本。