Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bind string array to a drop down list

Posted on 2006-06-13
12
Medium Priority
?
773 Views
Last Modified: 2012-05-05
What is the proper syntax for what I'm trying to do?  LinkUser returns a string array which I'm trying to bind to a dropdown list..

private void PopulateAgencies(){
      // populate dropdown list
      LinkUser me = new LinkUser();
      
      ddlAgencyCode.DataValueField      = me.agencyCodes[0];
      ddlAgencyCode.DataTextField      = me.agencyName[0].ToString().Trim();
      
      ddlAgencyCode.DataSource = me;
      ddlAgencyCode.DataBind();
}
0
Comment
Question by:russomr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
12 Comments
 
LVL 4

Expert Comment

by:svy
ID: 16896631
private void PopulateAgencies(){
     ddlAgencyCode.DataSource = new LinkUser().agencyCodes;
     ddlAgencyCode.DataBind();
}
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 1000 total points
ID: 16896655
I would do it like this

private void PopulateAgencies(){
for(int i = 0; i< agencyCodes.length;i++)
{
ListItem MyItem = new ListItem();
MyItem.Text = agencyCodes[i].ToString();
MyItem.Value = agencyCodes[i];
ddlAgencyCode.Items.Add(MyItem);
}

}
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896662
svy, how would you differentiate between a string value and an int value for the text and value properties? i.e. russomr seems to want an int for the value and a string for the text.

:-)

Andrew
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896668
My method enables you to apply each format
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16896673
sorry I mean each cast not format :-(
0
 
LVL 4

Expert Comment

by:svy
ID: 16897201
REA_ANDREW, ListItem.Value and ListItem.Text are both string type properties. I'm not sure what you're tryint to do with your code there...?

There's no need to specify DataTextField and DataValueField in this case.
0
 

Author Comment

by:russomr
ID: 16897220
svy,

the code

     ddlAgencyCode.DataSource = new LinkUser().agencyCodes;
     ddlAgencyCode.DataBind();

seems to work, accept how do I specify it so that the ddl value is the agencyCodes and what is displayed in the ddl is the agencyName?  Right now it just displays the agencyCodes.

Thanks.

0
 
LVL 4

Expert Comment

by:svy
ID: 16897675
private IEnumerable GetPairs(agencyCodes, agencyNames)
{
for(int i = 0; i< agencyCodes.Length && i < agencyNames.Length;i++)
yield return new Pair(agencyCodes[i], agencyNames[i]);
}
private void PopulateAgencies(){
     string [] agencyCodes = new LinkUser().agencyCodes;
     string [] agencyNames = new LinkUser().agencyNames;
     ddlAgencyCode.DataTextField = "Second";
     ddlAgencyCode.DataValueField = "First";
     ddlAgencyCode.DataSource = GetPairs(agencyCodes, agencyNames);
     ddlAgencyCode.DataBind();
}
0
 

Author Comment

by:russomr
ID: 16904376
I get this error message:

Compiler Error Message: CS1001: Identifier expected

(line 31 is the bolded line)

Source Error:

Line 29: }
Line 30:
Line 31: private IEnumerable GetPairs(agencyCodes, agencyNames){
Line 32:       for(int i = 0; i< agencyCodes.Length && i < agencyNames.Length;i++)
Line 33:       yield return new Pair(agencyCodes[i], agencyNames[i]);
 
I'm doing in-line coding.  Not sure if that makes a difference.
0
 

Author Comment

by:russomr
ID: 16904482
REA_ANDDREW,

I have used your code and applied it like so:

*********************************************
private void PopulateAgencies(){
      // populate dropdown list
      LinkUser me = new LinkUser();
      
      for(int i = 0; i< me.agencyCodes.length;i++){
            ListItem MyItem = new ListItem();
            MyItem.Text = me.agencyNames[i].ToString();
            MyItem.Value = me.agencyCodes[i].ToString();
            ddlAgencyCode.Items.Add(MyItem);
      }

}
*********************************************

Unfortunately, I get the following error:

*********************************************
Compiler Error Message: CS0117: 'System.Array' does not contain a definition for 'length'

Source Error:

Line 48:       LinkUser me = new LinkUser();
Line 49:       
Line 50:       for(int i = 0; i< me.agencyCodes.length;i++){
Line 51:             ListItem MyItem = new ListItem();
Line 52:             MyItem.Text = me.agencyNames[i].ToString();
*********************************************
 
Any thoughts?
0
 

Author Comment

by:russomr
ID: 16904524
Aaaah.  Capital L would help.  I think I got it going.  Thanks for all your help.
0
 
LVL 4

Expert Comment

by:svy
ID: 16905547
private IEnumerable GetItems(string[] agencyCodes, string[] agencyNames)
      {
            for (int i = 0; i < agencyCodes.Length && i < agencyNames.Length; i++)
                  yield return new ListItem(agencyNames[i], agencyCodes[i]);
      }
      private void PopulateAgencies()
      {
            string [] agencyCodes = new LinkUser().agencyCodes;
            string [] agencyNames = new LinkUser().agencyNames;

            ddlAgencyCode.DataTextField = "Text";
            ddlAgencyCode.DataValueField = "Value";
            ddlAgencyCode.DataSource = GetItems(agencyCodes, agencyNames);
            ddlAgencyCode.DataBind();
      }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question