C# 在c中显示CheckedListBox的ValueMember#

C# 在c中显示CheckedListBox的ValueMember#,c#,C#,我有一个绑定到表的CheckedListBox。我想从我选中的列表中获取任何项目的ValueMember。目前,我正在运行一个循环,遍历列表中所有选中的项目以显示ValueMember,因此这会显示我不想要的几个MessageBox。我想,在任何情况下,如果我检查任何项目,那么它应该向我显示其相应的ValueMember foreach (DataRowView view in clbAnnually.CheckedItems) { MessageBox.Show(view[clbAnnua

我有一个绑定到表的CheckedListBox。我想从我选中的列表中获取任何项目的ValueMember。目前,我正在运行一个循环,遍历列表中所有选中的项目以显示ValueMember,因此这会显示我不想要的几个MessageBox。我想,在任何情况下,如果我检查任何项目,那么它应该向我显示其相应的ValueMember

foreach (DataRowView view in clbAnnually.CheckedItems)
{
  MessageBox.Show(view[clbAnnually.ValueMember].ToString());
} 
我在SO中寻找过类似的问题,但他们并没有解决我的问题。请尽可能使用代码提供建议。

使用事件。当用户检查项目并提供有关已检查项目的所有信息时,会引发此问题

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;
一,

二,

使用事件。当用户检查项目并提供有关已检查项目的所有信息时,会引发此问题

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;
一,

二,


您上一个问题的答案在这种情况下没有帮助吗?您上一个问题的答案在这种情况下没有帮助吗?是的,先生,我在ItemCheck事件中使用了上面提到的foreach循环。但是我当前的代码给了我一系列我不想要的消息。为什么您要循环所有选中的项目?为什么不使用“e”事件参数中获得的参数?是否使用DataTable作为数据源?没有DataTable。我正在使用数据集和数据适配器绑定我的CLB@gomeshmunda那么您使用的是DataTable。是的,先生,我在ItemCheck事件中使用了上面提到的foreach循环。但是我现在的代码给了我一系列我不想要的消息。为什么要在所有选中的项目中循环?为什么不使用“e”事件参数中获得的参数?是否使用DataTable作为数据源?没有DataTable。我正在使用数据集和数据适配器绑定我的CLB@gomeshmunda然后您使用的是数据表。
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
    CheckedListBox clb = (CheckedListBox)sender;
    MessageBox.Show(clb.SelectedValue.ToString());
}