问题描述
我有一个绑定到 DataTable 的多选列表框.DataTable 包含 2 列描述和值.
I have a multi-select listbox which I am binding to a DataTable. DataTable contains 2 columns description and value.
这是列表框填充代码:
DataTable copytable = null;
copytable = GlobalTable.Copy(); // GlobalTable is a DataTable
copytable.Rows[0][0] = "--ALL--";
copytable.Rows[0][1] = "--ALL--";
breakTypeList.DataSource = copytable;
this.breakTypeList.DisplayMember = copytable.Columns[0].ColumnName; // description
this.breakTypeList.ValueMember = copytable.Columns[1].ColumnName; // value
this.breakTypeList.SelectedIndex = -1;
我将 description 设置为 DisplayMember,将 value 设置为 ListBox 的 ValueMember.现在根据传递的值,我需要在 ListBox 中设置所选项目.
I am setting description as the DisplayMember and value as the ValueMember of the ListBox. Now depending on what the value is passed I need to set the selected item in the ListBox.
这是我的代码:
ListBox lb = c as ListBox;
lb.SelectedValue = valuePassedByUser;
这不起作用.因此我不得不求助于下面的代码(我循环遍历列表框中的所有项目)
which is not working. Hence I have to resort to the code below (where I loop through all the items in the list box)
for (int i = 0; i < lb.Items.Count; i++)
{
DataRowView dr = lb.Items[i] as DataRowView;
if (dr["value"].ToString() == valuePassedByUser)
{
lb.SelectedIndices.Add(i);
break;
}
}
我想知道我的代码中缺少什么/错误.为什么是 lb.SelectedValue = valuePassedByUser;选择不正确的项目?
I would like to know what is missing/ erroneous in my code. Why is lb.SelectedValue = valuePassedByUser; selecting incorrect items?
推荐答案
好的......这是我昨天才意识到的难以理解的答案.虽然我没有在我的问题中提到一件重要的事情,但这是我的错误,因为我觉得它与手头的问题无关:
Ok ... here comes hard-to-digest answer which I realized only yesterday. It's my mistake though that I didn't mention one important thing in my question because I felt it is irrelevant to problem at hand:
数据表中的数据未排序.因此,我将列表框的 Sorted 属性设置为 true.后来我意识到当列表框甚至组合框的排序属性设置为 true 时,值成员没有正确设置.所以如果我写:
The data in the data table was not sorted. Hence I had set the listbox's Sorted property to true. Later I realized When the listbox's or even combo box's sorted property is set to true then the value member does not get set properly. So if I write:
lb.SelectedValue = valuePassedByUser;
它将某些其他项目设置为选中,而不是设置其值为 valuePassedByUser 的项目.简而言之,它与索引混淆.
it sets some other item as selected rather than settting the one whose Value is valuePassedByUser. In short it messes with the indexes.
例如如果我的初始数据是:
For e.g. if my initial data is:
Index ValueMember DisplayMember
1 A Apple
2 M Mango
3 O Orange
4 B Banana
我设置了 sorted = true.那么列表框项是:
And I set sorted = true. Then the listbox items are:
Index ValueMember DisplayMember
1 A Apple
2 B Banana
3 M Mango
4 O Orange
现在如果我想将 Banana 设置为选中,我运行 stmt:
Now if I want to set Banana as selected, I run the stmt:
lb.SelectedValue = "B";
但不是将 Banana 设置为选中,而是将 Orange 设置为选中.为什么?因为如果列表没有被排序,Banana 的索引会是 4.所以即使 Banana 的排序索引是 2 之后,它也会将索引 4 设置为选中,从而选择 Orange 而不是 Banana.
But instead of setting Banana as selected, it sets Orange as selected. Why? Because had the list not been sorted, index of Banana would be 4. So even though after sorting index of Banana is 2, it sets index 4 as selected, thus making Orange selected instead of Banana.
因此对于排序的列表框,我使用以下代码来设置选定项:
Hence for sorted listbox, I am using the following code to set selected items:
private void SetSelectedBreakType(ListBox lb, string value)
{
for (int i = 0; i < lb.Items.Count; i++)
{
DataRowView dr = lb.Items[i] as DataRowView;
if (dr["value"].ToString() == value)
{
lb.SelectedIndices.Add(i);
break;
}
}
}
这篇关于在列表框中设置所选项目而不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!