Solved

Bind string array to a drop down list

Posted on 2006-06-13
12
765 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
  • 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 250 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now