I need to know what is the type of every element in CheckedListBox.Items
? Is 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"]);
}