设置所选项目在ListBox不用循环 [英] Setting selected item in a ListBox without looping

查看:221
本文介绍了设置所选项目在ListBox不用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我绑定到一个DataTable多选列表框。 DataTable包含2列的描述和价值。

I have a multi-select listbox which I am binding to a DataTable. DataTable contains 2 columns description and value.

下面的列表框中填入代码:

Here's the listbox populating code:

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;



我设置描述,将DisplayMember和值列表框的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:

在数据表中的数据不排序。因此,我已经ListBox的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;

它设置一些其他项目的选择,而不是settting,其值是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

和我一组分类= 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

现在,如果我想为选中设定香蕉,我运行语句:

Now if I want to set Banana as selected, I run the stmt:

lb.SelectedValue = "B";

但不是为选中设置蕉,它集橙的选择。为什么?因为有了该列表没有排序,香蕉的指数是4,所以,即使排序香蕉的指数为2后,如选择,从而使选择的,而不是香蕉桔子它集指数4。

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;
        }
    }
}

这篇关于设置所选项目在ListBox不用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆