Iason
Iason

Reputation: 249

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);
}

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.

Edit: I bind to the ListView this way: I have a DataTable ds, and:

listBox1.DataSource = ds;

Upvotes: 0

Views: 633

Answers (4)

Ivan Stoev
Ivan Stoev

Reputation: 205939

For some unknown reason, DataSource, DisplayMember and ValueMember properties are hidden for CheckedListBox control.

If you want to copy the the list box items text, the correct way is to use ListControl.GetItemText method like this

foreach (var item in listBox1.Items)
    checkedListBox1.Items.Add(listBox1.GetItemText(item));

But this way it will be hard to find which source object is checked (for instance when enumerating CheckedItems). A better way would be to define your own class like this

class MyListItem
{
    public object Value;
    public string Text;
    public override string ToString() { return Text; }
}

and use

foreach (var item in listBox1.Items)
    checkedListBox1.Items.Add(new MyListItem { Value = item, Text = listBox1.GetItemText(item) });

Upvotes: 2

sujith karivelil
sujith karivelil

Reputation: 29036

You need to do a cast like the following :

foreach (var item in listBox1.Items)
 {
    checkedListBox1.Items.Add((ListItem)item);   
 }

or else you can use like this:

foreach (ListItem item in listBox1.Items)
 {
     checkedListBox1.Items.Add(item);   
 }

even this also may help you(use like this if you want text and value);

for(int i=0;i<listBox1.Items.Count-1;i++)
   {
      checkedListBox1.Items.Add(new ListItem() { Text = listBox1.Items[i].Text, Value = listBox1.Items[i].Text });   
   }

Upvotes: 1

Salah Akbari
Salah Akbari

Reputation: 39976

Try this:

foreach (var dataRowView in listBox1.Items.OfType<DataRowView>())
{
     checkedListBox1.Items.Add(dataRowView[0].ToString());
}

Upvotes: 1

Steve
Steve

Reputation: 216358

The text displayed by derived ListControl classes like a CheckedListBox, when these controls are binded to a datasource, is ruled by the property DisplayMember. This property equals to a string representing the name of a property (or a columnname) in the datasource.

So before adding the new items to your checkedlistbox I suggest to write

checkedListBox1.DataSource = listBox1.DataSource       
checkedListBox1.DisplayMember = listBox1.DisplayMember
checkedListBox1.ValueMember = listBox1.ValueMember

And no need to create a loop reading all the items from the source listbox, just use the same datasource and your are ready

Upvotes: 1

Related Questions