2009-09-04 152 views
0

我加入項目的AddRange使用方法下拉列表,這裏是我的代碼asp.net下拉列表

ListItem[] cou = 
new ListItem[8]{"India", 
       "United States", 
       "United Kingdom", 
       "Canada", 
       "Singapore", 
       "Australia", 
       "Sudia Arabia", 
       "South Africa" }; 
dpcountry.Items.AddRange(cou); 

,但它給我的錯誤是不能隱式轉換字符串TI的ListItem

請給我一個解決方案

在此先感謝 桑吉塔

+10

燦你在以前的7個問題中至少接受一個答案? – Shoban 2009-09-04 10:36:59

回答

1
object []cou = new object[]{"India", 
           "United States", 
           "United Kingdom", 
           "Canada", 
           "Singapore", 
           "Australia", 
           "Sudia Arabia", 
           "South Africa" }; 
    dpcountry.Items.AddRange(cou); 
3

您需要創建新的listItems中

嘗試

string[] cou =new string[8]{ 
       "India", 
       "United States", 
       "United Kingdom", 
       "Canada", 
       "Singapore", 
       "Australia", 
       "Sudia Arabia", 
       "South Africa" }; 
dpcountry.Items.AddRange(cou.Select(c => new ListItem(c)); 

您需要System.Linq的太參考,

善良,

1

你正在創建數組鍵入ListItem,但您正試圖向此數組添加字符串。這就是你得到這個錯誤的原因。要獲得此代碼工作,你應該將其更改爲:

new ListItem[8]{ new ListItem("India"), new ListItem("United"), /* etcetera */ };

2

我試過丹的例子,但不得不添加.ToArray()來得到它的工作,即:

string[] cou =new string[8]{ 
       "India", 
       "United States", 
       "United Kingdom", 
       "Canada", 
       "Singapore", 
       "Australia", 
       "Saudi Arabia", 
       "South Africa" }; 

dpcountry.Items.AddRange(cou.Select(c => new ListItem(c)).ToArray());