开发者

What is the type of every item in CheckedListBox.Items?

开发者 https://www.devze.com 2023-02-08 12:20 出处:网络
I need to know what is the type of every element in CheckedListBox.Items? I开发者_StackOverflow社区s it ListViewItem, Object or what?

I need to know what is the type of every element in CheckedListBox.Items? I开发者_StackOverflow社区s it ListViewItem, Object or what?

I also want to know how can I bind a DataTable to a CheckedListBox and set every items ID and text in windows forms?


The type depends on the objects that you use to fill the checked list box. You can use a DataTable by using the following code:

var table = new DataTable();

table.Columns.Add("ID");
table.Columns.Add("NAME");

table.Rows.Add("1", "John Doe");
table.Rows.Add("2", "Jane Doe");

this.checkedListBox1.DataSource = table;
this.checkedListBox1.ValueMember = "ID";
this.checkedListBox1.DisplayMember = "NAME";

In this case since you will be filling the checked list box using a DataTable doing this.checkedListBox1.CheckedItems after checking one or more items will output an ObjectCollection where each item in the collection is a DataRowView instance.

To obtain the checked items you could do:

var checkedIds = new List<string>();

foreach (var item in this.checkedListBox1.CheckedItems)
{
    var dataRowView = (DataRowView)item;

    checkedIds.Add((string)dataRowView["ID"]);
}

On a side note, you should really improve your acceptance rate.


The accepted answer didn't work for me; the state never changed. This is the code that I came up with that does work.

Private Sub SetCLBXState(ByVal DesiredState As Boolean)
    For ItemIndex As Integer = 0 To CLBXExclude.Items.Count - 1
        CLBXExclude.SetItemChecked(ItemIndex, DesiredState)
    Next
End Sub
0

精彩评论

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