我不完全确定您在寻找什么,我不确定意图是否完全清楚。 However I do believe you are binding data to two lists (I assume the same datasource, just different fields), and when an item in list 1 is selected, the corresponding item in list 2 will be selected.这是我的看法:
首先列表框必须分配数据;通过以不同方式绑定显示和值,您可以允许包含数据 ID 字段。
listBox1.DataSource = YourDatasource;
listBox1.ValueMember = YourIDField;
listBox1.DisplayMember = YourMessageOverview;
listBox2.DataSource = YourDatasource;
listBox2.ValueMember = YourIDField;
listBox2.DisplayMember = YourMessageText;
Then when the selection is changed, establish what the ID of the selected item is, then search the second list for an item with the same value.
protected void listbox1_SelectedIndexChanged(object sender, EventArgs e)
{
string val = (listBox1.SelectedItem as DataRowView)["columnName"].ToString();
listbox2.Items.FindByValue(val).Selected = true;
}
我没有检查过这段代码,但是我认为它应该不会有很多问题。
另外,对于 C# 对 VB 问题的回复,我深表歉意,我已经多年没有在 VB 中工作了。
根据这个stack answer更改了选定的项目值检索,希望有帮助吗?