2009-12-11 48 views
0

得到CheckedItems我有是有界到數據源爲foloowing checkedlistbox:從checkedlistbox

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

我想下面的代碼獲取checkeditems集合,但'無法投類型的對象System.Data.DataRowView '鍵入'System.String''錯誤發生。 :

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

有什麼問題?

回答

4

CheckedItems的內容不是字符串。

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