开发者

SetItemCheckState breaks foreach! Help?

I'm new here and have a question about C#'s CheckedListBox.

I built the CheckedListBox with data from a SQLite Database file.

I want the user to check or uncheck items and in doing so it updates in the database.

When you open the list again the Items you previously checked should still be checked. Ie if the boolean field in the database says "true" for a specific item, it should be checked.

Heres the code I'm using:

index = 0;
        foreach (DataRowView item in CheckedListBox.Items)
            {
                if (item.Row["viewed"].ToString() == "true")
                {                        
                   CheckedListBox.SetItemCheckState(index, CheckState.Checked);                     开发者_JS百科   
                }
                index++;
            }

When I comment out the line in the If statement the loop goes through all the items, but when i leave it like it is above, the loop only enters once.

Why is that?

I'm really new to C#.

Thank you for any help :)


Use for loop instead of foreach.

        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
            if (((DataRowView)checkedListBox1.Items[i]).Row["viewed"].ToString() == "true")
            {
                checkedListBox1.SetItemCheckState(i, CheckState.Checked);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜