【问题标题】:Get item index from databound DevExpress CheckedListBoxControl从数据绑定的 DevExpress CheckedListBoxControl 获取项目索引
【发布时间】:2012-02-18 15:58:12
【问题描述】:

我正在尝试从CheckedListBoxControl 中查找特定值的索引。 CheckedListBoxControl 有一个 DataSource、DisplayMember、ValueMember 设置为一个 DataTable 和两列接受。现在我必须通过使用 ValueMember 中的某个值从 CheckedListBoxControl 中查找其索引,然后使用该索引调用 SetItemChecked() 方法,将 CheckedState 属性设置为 true。

我找不到任何返回索引的属性或方法。请帮忙。

【问题讨论】:

    标签: winforms .net-2.0 devexpress checkedlistbox


    【解决方案1】:

    如果列表框控件绑定到数据源,您可以使用 GetItem() 方法和 ItemCount 属性遍历所有列表框项:

    for(int i = 0; i < checkedListBoxControl.ItemCount; i++) {
        object dataRow = checkedListBoxControl.GetItem(i);
    }
    

    要查找指定项的索引,您可以使用FindItem() 方法
    按 DisplayText 搜索:

    string s = "searchString";
    int index = checkedListBoxControl.FindItem(startIndex, true, delegate(ListBoxFindItemArgs e) {
       e.IsFound = s.Equals(e.DisplayText);
    });
    

    按 ValueMember 搜索:

    object value = 100;
    int index = checkedListBoxControl.FindItem(startIndex, true, delegate(ListBoxFindItemArgs e) {
       e.IsFound = object.Equals(value, e.ItemValue);
    });
    

    还请查看“How to get checked rows of a data-bound CheckedListBoxControl”文章。

    【讨论】:

    • 如果我只有 ValueMember 值怎么办?我认为并且我确信您的第二个示例不会起作用。无论如何感谢您的指出。
    • @Soham Dasgupta:我完全知道第二个样本按我的预期工作。无论如何,我已经更新了我的第二个示例,以便通过 ValueMember 进行搜索。
    • 这种语法似乎不起作用。请检查我是否错了。我正在使用 VB.Net,VS2008,框架版本:2.0 - checkedListBoxControl.FindItem(0, True, Function(e As ListBoxFindItemArgs) e.IsFound = Object.Equals(Value, e.ItemValue))
    • @Soham Dasgupta:顺便说一句,我在我的示例中使用 C#
    • 我只是无法理解为什么这不起作用。你能帮我吗。我认为这是您在 VB.NET 9.0 中编写内联 lambda 函数的唯一方法,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多