在“检查列表框”中获取索引值 [英] Get index with value in Checked List Box

查看:276
本文介绍了在“检查列表框”中获取索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上发现,当我浏览代码时, chkContactType.Items 是空的。我甚至添加了一个Watch到 chkContactType.Items.Count ,它从来没有任何东西,但我现在很严重混淆,因为显然不是我的插入方法工作正常使用这些相同的框,并为每个项目插入Value Member。

I am actually finding that chkContactType.Items is empty when I step through the code. I even added a Watch to chkContactType.Items.Count and 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....

我有一些检查列表框控件我需要根据项目值设置CheckState,因为这是存储在DB中的一个exsiting记录。不幸的是,我只看到一种通过索引设置的方法,该索引不存储。索引是控件的本地,所以例如,ControlType中有15个项目。索引为0-14。项目价值分别为39,40,41,42,43,44,45,46,47,48,49,50,2077,2078,2079。如何使用Value Member值获取索引值,或者使用Value Member值设置每个返回的项目的checkstate?

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?

谢谢

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



编辑回覆评论



这是我如何填充Checked ListBox。我知道那里有一个魔法数字我正在做。它与ContactType的DB中的CategoryID相关。

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.

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

然后CurrentCaseNote BLL(kinda) - >

and then CurrentCaseNote BLL(kinda)-->

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

最后是DAL - >

and finally the DAL -->

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



编辑2



这是我现在的代码,但仍然没有去。这就像ItemCount从来没有填充....

EDIT 2

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

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


推荐答案

似乎工作:

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

这篇关于在“检查列表框”中获取索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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