Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DropDownList in C# Application

DropDownList in C# Application

Scheduled Pinned Locked Moved C#
databasetutorialcsharpdata-structuresquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gavin_Mannion
    wrote on last edited by
    #1

    Hi All, I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field. Example: :) My Drop Down list has the following Option 1: London Option 2: Liverpool Option 3: Other If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool. In ASP and VB you where just able to add a value to the field, can this be done in C#? Thanks,

    C S 2 Replies Last reply
    0
    • G Gavin_Mannion

      Hi All, I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field. Example: :) My Drop Down list has the following Option 1: London Option 2: Liverpool Option 3: Other If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool. In ASP and VB you where just able to add a value to the field, can this be done in C#? Thanks,

      C Offline
      C Offline
      Christopher Lord
      wrote on last edited by
      #2

      If your working in ASP.net, one of the the Add overrides lets you specify a ListItem class, which takes two strings. These map to the text of the option and the value of the option in html. Value can be retrieved from SelectedItem.Value, and the text from SelectedItem.Text. I expect C# for apps is fairly close, but there are definately differences between the two. One difference I expect is the ability to map an object to the string instead of a second string. // Rock

      G 1 Reply Last reply
      0
      • C Christopher Lord

        If your working in ASP.net, one of the the Add overrides lets you specify a ListItem class, which takes two strings. These map to the text of the option and the value of the option in html. Value can be retrieved from SelectedItem.Value, and the text from SelectedItem.Text. I expect C# for apps is fairly close, but there are definately differences between the two. One difference I expect is the ability to map an object to the string instead of a second string. // Rock

        G Offline
        G Offline
        Gavin_Mannion
        wrote on last edited by
        #3

        Using ASP I have been able to do it and that is why I expect it to be at least similar in a C# Application. There just doesn't seem to be a possible option for value which is mind boggling if they have decided to scrap that one. Please let me know if you come across anything that explains this, also how do you mean CLaW wrote: One difference I expect is the ability to map an object to the string instead of a second string. ???? Thanks

        1 Reply Last reply
        0
        • G Gavin_Mannion

          Hi All, I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field. Example: :) My Drop Down list has the following Option 1: London Option 2: Liverpool Option 3: Other If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool. In ASP and VB you where just able to add a value to the field, can this be done in C#? Thanks,

          S Offline
          S Offline
          SimonS
          wrote on last edited by
          #4

          Hi Gavin The ComboBox in .NET receives a generic object. This is pretty powerful, but also is quite a change to the normal VB-style. All that's required is to create a simple (or more complex) class and use that to add to the ComboBox. To get values out, just cast to your object and call your custom props. The ToString() override caught me out, but the ComboBOx uses it to show values. private void Form1_Load(object sender, System.EventArgs e) { comboBox1.Items.Clear(); comboBox1.Items.Add(new cComboLoader("Bill",1)); comboBox1.Items.Add(new cComboLoader("Steve",2)); comboBox1.Items.Add(new cComboLoader("Paul",3)); comboBox1.Items.Add(new cComboLoader("Reinout",4)); } private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).ToString()); MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).myIndexVal().ToString()); } public class cComboLoader { private string _Txt=""; private int _iVal=0; public cComboLoader(string sTxt, int iVal) { _Txt=sTxt; _iVal=iVal; } public override string ToString() { return _Txt; } public int myIndexVal() { return _iVal; } } Does this help you? Cheers, Simon X-5 452 rules.

          G 1 Reply Last reply
          0
          • S SimonS

            Hi Gavin The ComboBox in .NET receives a generic object. This is pretty powerful, but also is quite a change to the normal VB-style. All that's required is to create a simple (or more complex) class and use that to add to the ComboBox. To get values out, just cast to your object and call your custom props. The ToString() override caught me out, but the ComboBOx uses it to show values. private void Form1_Load(object sender, System.EventArgs e) { comboBox1.Items.Clear(); comboBox1.Items.Add(new cComboLoader("Bill",1)); comboBox1.Items.Add(new cComboLoader("Steve",2)); comboBox1.Items.Add(new cComboLoader("Paul",3)); comboBox1.Items.Add(new cComboLoader("Reinout",4)); } private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).ToString()); MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).myIndexVal().ToString()); } public class cComboLoader { private string _Txt=""; private int _iVal=0; public cComboLoader(string sTxt, int iVal) { _Txt=sTxt; _iVal=iVal; } public override string ToString() { return _Txt; } public int myIndexVal() { return _iVal; } } Does this help you? Cheers, Simon X-5 452 rules.

            G Offline
            G Offline
            Gavin_Mannion
            wrote on last edited by
            #5

            Wow, just a little bit more complicated than VB :)... Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine :).. What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID. Hey it works ;p Thanks for the help though

            S J 2 Replies Last reply
            0
            • G Gavin_Mannion

              Wow, just a little bit more complicated than VB :)... Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine :).. What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID. Hey it works ;p Thanks for the help though

              S Offline
              S Offline
              SimonS
              wrote on last edited by
              #6

              You can easily combine the 2 solutions by using the temp. inline cComboLoader class in your Combo.Items.Add. What will happen if you get 2 values with the same text, but diff. ID's? Cheers, Simon X-5 452 rules.

              1 Reply Last reply
              0
              • G Gavin_Mannion

                Wow, just a little bit more complicated than VB :)... Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine :).. What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID. Hey it works ;p Thanks for the help though

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                There is another way, it involves using the DataSource, DisplayMember and ValueMember properties of the combo box. If I weren't late for work I'd describe exactly how to use it, but if you search the docs for DisplayMember there is an example of using it :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups