tags:

views:

202

answers:

1

hi i have checkedlistbox which is bounded to a datasource as foloowing :

     chListBox.DataSource = dsContacts.Tables["Contacts"];
     chListBox.DisplayMember = "FullName";
     chListBox.ValueMember = "ContactNumber";

i want to get checkeditems collection by following code , but 'Unable to cast object of type 'System.Data.DataRowView' to type 'System.String' ' error occurs . :

        int i = 0;
        foreach (string row in chListBox.CheckedItems)
        {
            phoneNumbers[i] = row.ToString();
            i++;
        }

what is the problem ?

+2  A: 

The contents of CheckedItems isn't strings.

    int i = 0; 
    foreach (DataRowView rowView in chListBox.CheckedItems) 
    { 
        phoneNumbers[i] = rowView["ContactNumber"]; 
        i++; 
    }
Mark Byers