如何将选中的项目从选中的列表框打印到c#中的标签?

问题描述 投票:-1回答:1

我正在尝试从检查列表框中获取项目,当用户检查列表框项目时,它应该在按钮单击的标签中显示。我试过用这个:

foreach (object item in checkedlistbox1.CheckedItems)
{
    labelto.Text += checkedlistbox1.SelectedItem.ToString();
}

但我得到了这个例外:

 List that this enumerator is bound to has been found, enumerator can only be used if the list does not change.

如何将选中的项目从选中的列表框打印到标签?

c# checkedlistbox
1个回答
0
投票

这不是一项复杂的任务,为什么不试试以下内容:

string displayText = "";
foreach(object item in checkedListBox1.CheckedItems)
{
     DataRowView castedItem = item as DataRowView;
     displayText += castedItem["boundPropertyNameHere"];  
}
labelto.Text = displayText;

请注意:

  • 其中boundPropertyNameHere是用于绑定集合的属性的名称。
© www.soinside.com 2019 - 2024. All rights reserved.