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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:21:40  来源:igfitidea点击:

How to select value on a databound combobox?

vb.netdata-bindingcombobox

提问by Zolomon

I have a ComboBoxthat's been bound to a DataViewthat's taken a filled DataTable. This DataTablehas three columns. Before the DataViewis bound to the ComboBoxI 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 DataTableand bind it to a DataGridViewin 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 ComboBoxrow that has the same value, that I set from the second form, tied to its ValueMemberproperty. I thought that SelectedValuewould 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 SelectedItemeither. 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 属性更改为您输入的文本。