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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. SelectedValue and comboboxen

SelectedValue and comboboxen

Scheduled Pinned Locked Moved C#
helpdebuggingquestion
5 Posts 3 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.
  • R Offline
    R Offline
    redspiderke
    wrote on last edited by
    #1

    Hi, I want to fill the 2nd combobox with values dependent on the value from the first combobox In reallife: The first combo contains car brands, the second a list of types. I want that when I select Ford I only get the types from Ford

    // Vul comboboxes
    private void GetVtgMerken()
    {
    txtBrand.DataSource = _Brand.GetVtgBrand();
    txtBran.DisplayMember = "Brand_Name";
    txtBran.ValueMember = "Brand_ID";
    }
    private void GetVtgBrandTypes(int ID)
    {
    txtType.DataSource = _Brandtype.GetVtgBrandTypes(ID);
    txtType.DisplayMember = "type_Naam";
    txtType.ValueMember = "MT_Id";
    }

        // Fill the second combo 
        private void txtBrand\_SelectedIndexChanged(object sender, EventArgs e)
        {
            // first debug
            textBox1.Text = txtBrand.SelectedValue.ToString();
    
                 // first attempt
                 int ID = Convert.ToInt32(txtBrand.SelectedValue.ToString());
                 // second attempt
                 int ID = Int32.Parse(txtBrand.SelectedValue.ToString());
                 
           // the new select
            GetVtgMerkTypes(ID);
        }
    

    When I use Convert.toInt32 or int32.parse I always get the error like below. System.FormatException: Input string was not in a correct format. Textbox1.text displays the value correct (ex. 3) Is there someone who can help ?

    V L 2 Replies Last reply
    0
    • R redspiderke

      Hi, I want to fill the 2nd combobox with values dependent on the value from the first combobox In reallife: The first combo contains car brands, the second a list of types. I want that when I select Ford I only get the types from Ford

      // Vul comboboxes
      private void GetVtgMerken()
      {
      txtBrand.DataSource = _Brand.GetVtgBrand();
      txtBran.DisplayMember = "Brand_Name";
      txtBran.ValueMember = "Brand_ID";
      }
      private void GetVtgBrandTypes(int ID)
      {
      txtType.DataSource = _Brandtype.GetVtgBrandTypes(ID);
      txtType.DisplayMember = "type_Naam";
      txtType.ValueMember = "MT_Id";
      }

          // Fill the second combo 
          private void txtBrand\_SelectedIndexChanged(object sender, EventArgs e)
          {
              // first debug
              textBox1.Text = txtBrand.SelectedValue.ToString();
      
                   // first attempt
                   int ID = Convert.ToInt32(txtBrand.SelectedValue.ToString());
                   // second attempt
                   int ID = Int32.Parse(txtBrand.SelectedValue.ToString());
                   
             // the new select
              GetVtgMerkTypes(ID);
          }
      

      When I use Convert.toInt32 or int32.parse I always get the error like below. System.FormatException: Input string was not in a correct format. Textbox1.text displays the value correct (ex. 3) Is there someone who can help ?

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      What's the value of txtBrand.SelectedValue.ToString() ? Check if it is really a number (meaning digits only) if it contains letters, you'll need to perform additional formatting or use another container then int for ID.

      V.

      R 1 Reply Last reply
      0
      • R redspiderke

        Hi, I want to fill the 2nd combobox with values dependent on the value from the first combobox In reallife: The first combo contains car brands, the second a list of types. I want that when I select Ford I only get the types from Ford

        // Vul comboboxes
        private void GetVtgMerken()
        {
        txtBrand.DataSource = _Brand.GetVtgBrand();
        txtBran.DisplayMember = "Brand_Name";
        txtBran.ValueMember = "Brand_ID";
        }
        private void GetVtgBrandTypes(int ID)
        {
        txtType.DataSource = _Brandtype.GetVtgBrandTypes(ID);
        txtType.DisplayMember = "type_Naam";
        txtType.ValueMember = "MT_Id";
        }

            // Fill the second combo 
            private void txtBrand\_SelectedIndexChanged(object sender, EventArgs e)
            {
                // first debug
                textBox1.Text = txtBrand.SelectedValue.ToString();
        
                     // first attempt
                     int ID = Convert.ToInt32(txtBrand.SelectedValue.ToString());
                     // second attempt
                     int ID = Int32.Parse(txtBrand.SelectedValue.ToString());
                     
               // the new select
                GetVtgMerkTypes(ID);
            }
        

        When I use Convert.toInt32 or int32.parse I always get the error like below. System.FormatException: Input string was not in a correct format. Textbox1.text displays the value correct (ex. 3) Is there someone who can help ?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        conversion methods will throw a FormatException when the input string isn't in the right format, which includes the case where the input is empty. e.g. you may get that while your Form gets loaded. It is often solved easily by including a non-zero-length test in your code. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        R 1 Reply Last reply
        0
        • V V 0

          What's the value of txtBrand.SelectedValue.ToString() ? Check if it is really a number (meaning digits only) if it contains letters, you'll need to perform additional formatting or use another container then int for ID.

          V.

          R Offline
          R Offline
          redspiderke
          wrote on last edited by
          #4

          When I do:

          textBox1.text = txtBrand.SelectedValue.ToString()

          the textbox shows the value "3". An integer I think :) But when I convert the textbox to an variable the debugger complains.

          Kan een object van het type System.Int32 niet converteren naar het type System.Data.DataRowView.

          After a long search i fixt it like this:

          private void txtBrand_SelectedIndexChanged(object sender, EventArgs e)
          {
          if (txtBrand.SelectedValue is DataRowView)
          {
          string BrandID = Convert.ToInt32(((DataRowView)txtBrand.SelectedValue)["Brand_Id"]).ToString();
          int ID = Convert.ToInt32(BrandID);
          GetVtgMerkTypes(ID);
          }
          else
          {
          int ID = Convert.ToInt32(txtBrand.SelectedValue.ToString());
          GetVtgMerkTypes(ID);
          }
          }

          ... this works :-)

          1 Reply Last reply
          0
          • L Luc Pattyn

            conversion methods will throw a FormatException when the input string isn't in the right format, which includes the case where the input is empty. e.g. you may get that while your Form gets loaded. It is often solved easily by including a non-zero-length test in your code. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            R Offline
            R Offline
            redspiderke
            wrote on last edited by
            #5

            Luc, I think you are right !!! I fixt it with the code below:

                private void txtBrand\_SelectedIndexChanged(object sender, EventArgs e)
                {
                    if (txtBrand.SelectedValue is DataRowView)
                    {
                        string BrandID = Convert.ToInt32(((DataRowView)txtBrand.SelectedValue)\["Brand\_Id"\]).ToString();
                        int ID = Convert.ToInt32(BrandID);
                        GetVtgBrandTypes(ID);
                    }
                    else
                    {
                        int ID = Convert.ToInt32(txtBrand.SelectedValue.ToString());
                        GetVtgBrandTypes(ID);
                    }
                }
            

            maybe not nice but it works :)

            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