问题
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);
}
}
回答1:
This seems to work:
int index = checkedListBox1.Items.IndexOf("42");
checkedListBox1.SetItemChecked(index, true);
回答2:
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
回答3:
I also faced the same issue.
The index of checkedListBox1.Items.IndexOf("value");
is always -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);
}
来源:https://stackoverflow.com/questions/1111558/get-index-with-value-in-checked-list-box