2010-02-25 37 views
0

我有這個C#代碼。從數據集添加列表框項目

j = myAccountDataset.Tables["AccountsTables"].Rows.Count; 

       for (i = 0; i <= (j - 1); i++) 
       { 

        listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1]); 
       } 

       this.listAccountList.SelectedIndex = 0; 

這個想法是迭代數據集內部並將項目添加到列表中。 但我收到以下錯誤:錯誤 1「System.Web.UI.WebControls.ListItemCollection.Add(串)」的最佳重載的方法匹配具有一些無效參數

ARGUMENT1:無法從「對象」轉換'串'

我一定在做錯事。 thye error is in the line:listAccountList.Items.Add(myAccountDataset.Tables [0] .Rows [i] [1]);

謝謝。

回答

1

ListItemCollection的Add方法只接受兩種類型 - 一個字符串或一個ListItem 請參閱MSDN文檔here。你需要傳遞一個字符串,而不是一個對象:

listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1].ToString()); 
+0

非常感謝。有用 !! – tika 2010-02-25 02:26:58

0

多一點的描述

你myAccountDataset.Tables [0] .Rows [I] [1]是一個無類型的對象,Add方法是期望一個字符串,你需要將該對象轉換爲一個字符串。最簡單的方法是將.ToString()運算符添加到您的數據對象中。

myAccountDataset.Tables[0].Rows[i][1].Tostring() 
+0

謝謝,它的工作原理。 – tika 2010-02-25 02:28:51