C# 在选中的列表框中获取具有值的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1111558/
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-08-06 08:30:24  来源:igfitidea点击:

Get index with value in Checked List Box

c#winformsdata-bindingdevexpresscheckedlistbox

提问by Refracted Paladin

I am actually finding that chkContactType.Itemsis empty when I step through the code. I even added a Watch to chkContactType.Items.Countand it is never anything but 0. I am severly confused now as it obviously isn't as my Insert method works fine which uses these same boxes and inserts the Value Member for each item....

chkContactType.Items当我逐步执行代码时,我实际上发现它是空的。我什至添加了一个 WatchchkContactType.Items.Count并且它从来都不是 0。我现在很困惑,因为它显然不是因为我的 Insert 方法工作正常,它使用这些相同的框并为每个项目插入值成员......



I have some checked list box controls that I need to set the CheckState based on the item value as that is what is stored in the DB for an exsiting record. Unfortunately, I only see a way to set this by index which is not stored. The index is local to the control so, for example, control ContactType has 15 items in it. Index is 0-14. Item Value is 39,40,41,42,43,44,45,46,47,48,49,50,2077,2078,2079 respectively. How can I either get the index value with the Value Member value OR set the checkstate of each returned item with the Value Member value?

我有一些选中的列表框控件,我需要根据项目值设置 CheckState,因为这是存储在数据库中的现有记录。不幸的是,我只看到了一种通过未存储的索引来设置它的方法。索引是控件的本地索引,因此,例如,控件 ContactType 中有 15 个项目。指数为 0-14。项目价值分别为39、40、41、42、43、44、45、46、47、48、49、50、2077、2078、2079。如何使用值成员值获取索引值或使用值成员值设置每个返回项目的检查状态?

Thanks

谢谢

 private void PaintDetails(Guid cNoteID)
    {
        var cNoteDetailDT = CurrentCaseNote.GetCNoteDetail(cNoteID);
        LoadCaseNoteDetailData(cNoteDetailDT.Rows[0]);

        // Load Contact Type Data for this CaseNote
        // contactTypeDT returns ItemID of chk items 
        // that were checked for this Guid
        using (var contactTypeDT = CurrentCaseNote.GetCNoteContactType(cNoteID))
        {
            if (contactTypeDT.Rows.Count > 0)
                foreach (DataRow row in contactTypeDT.Rows)
                {
                    LoadContactTypeData(row);
                }
        }
    }

    private void LoadContactTypeData(DataRow row)
    {
        // This does not work
        var theItem = row["ItemID"].ToString();
        // itemIndex always ends up set to -1
        var itemIndex = chkContactType.Items.IndexOf(theItem);
        chkContactType.SetItemChecked((int) itemIndex, true);

        // This works I just need to supply the correct index
        chkContactType.SetItemChecked(0,true);
    }

EDIT in response to comment

编辑以回应评论

This is how I populate the Checked ListBox. I know there is a "magic number" there. I am working on it. It relates to the CategoryID in the DB of ContactType.

这就是我填充选中列表框的方式。我知道那里有一个“幻数”。我正在做。它与 ContactType 的 DB 中的 CategoryID 相关。

 // Contact Type Check List Box
        chkContactType.DataSource = CurrentCaseNote.GetMaintItems(1);
        chkContactType.DisplayMember = "ItemDescription";
        chkContactType.ValueMember = "ItemID";

and then CurrentCaseNote BLL(kinda)-->

然后 CurrentCaseNote BLL(有点)-->

public static DataTable GetMaintItems(int iCat)
    {
        IQueryable<tblCaseNotesMaintItem> tItems = CaseNoteDAL.GetCNTable();
        return (tItems.Where(item => item.CategoryID == iCat & item.IsActive).OrderBy(
                               item => item.OrderID).Select(item => new {item.ItemID, item.ItemDescription})).CopyLinqToDataTable();
    }

and finally the DAL -->

最后是 DAL -->

        public static Table<tblCaseNotesMaintItem> GetCNTable()
    {
        return dcCaseNotes.GetTable<tblCaseNotesMaintItem>();
    }

EDIT 2

编辑 2

This is what my code NOW looks like but still no go. It is like ItemCount is never populated....

这就是我的代码现在的样子,但仍然不行。就像 ItemCount 永远不会被填充....

            // Load Contact Type Data for this CaseNote
        using (var contactTypeDT = CurrentCaseNote.GetCNoteContactType(cNoteID))
        {
            if (contactTypeDT.Rows.Count > 0)
                foreach (DataRow row in contactTypeDT.Rows)
                {
                    LoadContactTypeData(row);
                }
        }
    }

    private void LoadContactTypeData(DataRow row)
    {
        // This does not work
        var theItem = row["ItemID"];

        for (int i = 0; i < chkContactType.ItemCount; i++)
        {
            if(theItem == chkContactType.GetItemValue(i))
                chkContactType.SetItemChecked(i,true);
        }
    }

采纳答案by Don Kirkby

This seems to work:

这似乎有效:

int index = checkedListBox1.Items.IndexOf("42");
checkedListBox1.SetItemChecked(index, true);

回答by Dattatray Dongare

                For j As Integer = 0 To chklst_distributorlist.Items.Count - 1

                    If chklst_distributorlist.GetItemText(chklst_distributorlist.Items.Item(j)) = ds1.Tables(0).Rows(0)("suppliername").ToString Then

                        chklst_distributorlist.SetSelected(j, True)
                        chklst_distributorlist.SetItemCheckState(j, CheckState.Checked)
                    End If
                Next

回答by Abhilash

I also faced the same issue.

我也面临同样的问题。

The index of checkedListBox1.Items.IndexOf("value");is always -1.

的索引checkedListBox1.Items.IndexOf("value");始终为-1

I managed to get the index using following code.

我设法使用以下代码获取索引。

for (int i = 0; i < checkedListBox1.Items.Count; i++)   
{    
   if (((System.Data.DataRowView)(checkedListBox1.Items[i])).Row.ItemArray[0].ToString() == "Value")    

   checkedListBox1.SetItemCheckState(i, CheckState.Checked);    
}