I have a CheckedListBox which is bound to a table.I am wanting to get the ValueMember of any item from the list which i check.Presently i am running a loop through all the checked items of the list to show the ValueMember, as a result this shows me several messageboxes which i don't want. I want, at any instance if i check any item then it should show me its corrresponding ValueMember.My current code is
foreach (DataRowView view in clbAnnually.CheckedItems)
{
MessageBox.Show(view[clbAnnually.ValueMember].ToString());
}
I have looked for similar question in SO, but they didn't solve my problem. Please advice possibly with code.
Use the CheckedListBox.ItemCheck event. It's raised when a user checks an item and provides all information about the checked item.
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name");
table.Rows.Add(0, "Name 0");
table.Rows.Add(1, "Name 1");
table.Rows.Add(2, "Name 2");
checkedListBox1.DataSource = table;
checkedListBox1.DisplayMember = "Name";
checkedListBox1.ValueMember = "ID";
checkedListBox1.ItemCheck += CheckedListBox1_ItemCheck;
1.
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
CheckedListBox clb = (CheckedListBox)sender;
DataRowView row = (DataRowView)clb.Items[e.Index];
MessageBox.Show(row[clb.ValueMember].ToString());
}
2.
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
CheckedListBox clb = (CheckedListBox)sender;
MessageBox.Show(clb.SelectedValue.ToString());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.