获取与价值指数在经过列表框 [英] Get index with value in Checked List Box

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

问题描述

其实我发现, chkContactType.Items 是空的,当我通过代码。我甚至增加了一个手表 chkContactType.Items.Count ,这是从来没有什么,但0我severly困惑,现在,因为它显然不是我的插入法正常工作而使用这些相同的箱子,并插入值的成员为每个项目....






我有一些检查,我需要设置根据项目价值CheckState因为这是被存储在数据库中的已经存在的记录列表框控件。不幸的是,我只看到的方式由未存储索引设置此。该指数是本地的控制,使得,例如,控制ContactType中有15个项目。指数是0-14。项目值分别为39,40,41,42,43,44,45,46,47,48,49,50,2077,2078,2079。我无论怎样才能获得与value成员值的索引值或设定值的成员值返回的每个项目的checkstate?



感谢

 私人无效PaintDetails(GUID cNoteID)
{
VAR cNoteDetailDT = CurrentCaseNote.GetCNoteDetail(cNoteID);
LoadCaseNoteDetailData(cNoteDetailDT.Rows [0]);

//装载联系人类型数据本CaseNote
// contactTypeDT返回使用(VAR contactTypeDT进行检查此GUID
的CHK项目
// itemid的= CurrentCaseNote.GetCNoteContactType(cNoteID))
{
如果(contactTypeDT.Rows.Count大于0)
的foreach(DataRow的行contactTypeDT.Rows)
{
LoadContactTypeData (行);
}
}
}

私人无效LoadContactTypeData(DataRow的行)
{
//这不起作用
变种theItem =行[项目编号]的ToString()。
//的ItemIndex最终总是设置为-1
变种的ItemIndex = chkContactType.Items.IndexOf(theItem);
chkContactType.SetItemChecked((INT)的ItemIndex,真正的);

//这工作我只需要提供正确的索引
chkContactType.SetItemChecked(0,真正的);
}



编辑回应置评



这是我如何填充列表框选中。我知道有一个幻数在那里。我正在做。它涉及到在类别ID的ContactType数据库。

  //联系类型选中列表框
chkContactType.DataSource = CurrentCaseNote.GetMaintItems(1);
chkContactType.DisplayMember =ItemDescription;
chkContactType.ValueMember =项目编号;



然后CurrentCaseNote BLL(有点) - >

 公共静态数据表GetMaintItems(INT ICAT)
{
&IQueryable的LT; tblCaseNotesMaintItem> tItems = CaseNoteDAL.GetCNTable();
回报率(tItems.Where(项目=> item.CategoryID == ICAT&安培; item.IsActive).OrderBy(
项= GT; item.OrderID)。选择(项目=>新建{ item.ItemID,item.ItemDescription}))CopyLinqToDataTable()。
}

和最后的DAL - >

 公共静态表< tblCaseNotesMaintItem> GetCNTable()
{
返回dcCaseNotes.GetTable< tblCaseNotesMaintItem>();
}



编辑2



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

  //装载联系类型数据使用此CaseNote 
(变种contactTypeDT = CurrentCaseNote.GetCNoteContactType(cNoteID))
{
如果(contactTypeDT.Rows.Count大于0)
的foreach(DataRow的行中contactTypeDT.Rows)
{
LoadContactTypeData(行);
}
}
}

私人无效LoadContactTypeData(DataRow的行)
{
//这不起作用
变种theItem =行[项目编号];

的for(int i = 0; I< chkContactType.ItemCount;我++)
{
如果(theItem == chkContactType.GetItemValue(I))
chkContactType .SetItemChecked(我,真);
}
}


解决方案

这似乎工作:

  INT指数= checkedListBox1.Items.IndexOf(42); 
checkedListBox1.SetItemChecked(指数,真正的);


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....


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?

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.

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

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

and finally the DAL -->

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

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

解决方案

This seems to work:

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

这篇关于获取与价值指数在经过列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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