将项目从列表框复制到CheckedListBox [英] Copy items from ListBox to CheckedListBox
问题描述
有很多问题与这个问题相反,但是不幸的是,没有一个问题对我有用.我有以下代码可以实现我的目的:
There are many questions that ask the opposite of this question, unfortunately none of them have worked for me. I have the following code to achieve my purpose:
foreach (var item in listBox1.Items)
{
checkedListBox1.Items.Add(item);
}
问题是当我这样做时,我没有得到 ListBox
内的值,而是 System.Data.DataRowView
项内的值.因此,我的 CheckedListBox
正是使用 System.Data.DataRowView
字符串填充的,它们都是相同的,并且不会显示实际的字符串值.
The problem is that when I do this, I don't get the values inside the ListBox
, rather the System.Data.DataRowView
items. So my CheckedListBox
gets populated with exactly this, System.Data.DataRowView
strings, which are all the same and don't show the actual string value.
编辑:我这样绑定到 ListView
:我有一个 DataTable ds
,并且:
Edit: I bind to the ListView
this way: I have a DataTable ds
, and:
listBox1.DataSource = ds;
推荐答案
尝试一下:
foreach (var dataRowView in listBox1.Items.OfType<DataRowView>())
{
checkedListBox1.Items.Add(dataRowView[0].ToString());
}
这篇关于将项目从列表框复制到CheckedListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!