0

i am adding items to dropdownlist using addrange method, here is my code

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

but it gives me error as cannot implicitly convert string ti listitem

please give me a solution

thanks in advance sangita

1
  • 10
    Can you atleast accept one answer in your previous 7 questions? Commented Sep 4, 2009 at 10:36

4 Answers 4

3

You need to create new ListItems

try

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

You will need a reference to System.Linq too,

Kindness,

Dan

Sign up to request clarification or add additional context in comments.

Comments

2

I tried Dan's example, but had to add .ToArray() to get it to work, ie:

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

Comments

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

Comments

1

You are creating an array of type ListItem, but you are trying to add strings to this array. That is why you get this error. To get this code to work, you should change it to:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.