views:

2984

answers:

2

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);
        }
    }
+2  A: 

This seems to work:

int index = checkedListBox1.Items.IndexOf("42");
checkedListBox1.SetItemChecked(index, true);
Don Kirkby
That is what I would think as well. I wonder if my issue is elsewhere as when I step thru the code it appears as if checkedListBox.Items is empty....
Refracted Paladin
Have you got it bound to a binding data source? That might not populate the Items property. You might have to go through the binding data source's interface to find the index you want.
Don Kirkby
+1  A: 

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
Dattatray Dongare