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. ComboBoxes and DataGridViews...

ComboBoxes and DataGridViews...

Scheduled Pinned Locked Moved C#
4 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.
  • I Offline
    I Offline
    Illegal Operation
    wrote on last edited by
    #1

    Hi, I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:

        private void PopulateCountries()
        {
            cbCountryId.DataSource = dsCountries.Tables\["Country"\];
            cbCountryId.DisplayMember = "Country";
            cbCountryId.ValueMember = "Id";
        }
    

    What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:

        private void GetStateByCountryId(int CountryId)
        {
            this.dsWBGT\_V4 = dataCommunicator.GetStateByCountryId(CountryId);
            this.stateBindingSource.DataSource = this.dsWBGT\_V4;
        }
    

    As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:

    CountryId = Convert.ToInt32(cbCountryId.SelectedValue);

    Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??

    Illegal Operation

    M A 2 Replies Last reply
    0
    • I Illegal Operation

      Hi, I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:

          private void PopulateCountries()
          {
              cbCountryId.DataSource = dsCountries.Tables\["Country"\];
              cbCountryId.DisplayMember = "Country";
              cbCountryId.ValueMember = "Id";
          }
      

      What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:

          private void GetStateByCountryId(int CountryId)
          {
              this.dsWBGT\_V4 = dataCommunicator.GetStateByCountryId(CountryId);
              this.stateBindingSource.DataSource = this.dsWBGT\_V4;
          }
      

      As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:

      CountryId = Convert.ToInt32(cbCountryId.SelectedValue);

      Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??

      Illegal Operation

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      In your selectedindexchanged event test that there is a valid item if(DGV.SelectedItems.count == 0) {return ;} This is a common issue - an index changes twice - deselects on [0] and selects[4] so the event fires twice. This is needed for all collection controls, you just need to find the correct property to test for each control type.

      Never underestimate the power of human stupidity RAH

      I 1 Reply Last reply
      0
      • M Mycroft Holmes

        In your selectedindexchanged event test that there is a valid item if(DGV.SelectedItems.count == 0) {return ;} This is a common issue - an index changes twice - deselects on [0] and selects[4] so the event fires twice. This is needed for all collection controls, you just need to find the correct property to test for each control type.

        Never underestimate the power of human stupidity RAH

        I Offline
        I Offline
        Illegal Operation
        wrote on last edited by
        #3

        Thank you for the reply. Excuse my ignorance but I have tried to add this code to the IndexChange event but I have no option of SelectedItems.count? I have tried dgvState.SelectedRows.Count but obviously that does not work?

        Illegal Operation

        1 Reply Last reply
        0
        • I Illegal Operation

          Hi, I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:

              private void PopulateCountries()
              {
                  cbCountryId.DataSource = dsCountries.Tables\["Country"\];
                  cbCountryId.DisplayMember = "Country";
                  cbCountryId.ValueMember = "Id";
              }
          

          What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:

              private void GetStateByCountryId(int CountryId)
              {
                  this.dsWBGT\_V4 = dataCommunicator.GetStateByCountryId(CountryId);
                  this.stateBindingSource.DataSource = this.dsWBGT\_V4;
              }
          

          As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:

          CountryId = Convert.ToInt32(cbCountryId.SelectedValue);

          Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??

          Illegal Operation

          A Offline
          A Offline
          a hamidy
          wrote on last edited by
          #4

          you can ignore the selectedIndexChanged Event whenever you want. follow the steps to ignore the event and put it back when the form completly load. comboBox1.SelectedIndexChanged -=comboBox1_selectedIndexChanged; do your work here and then comboBox1.SelectedIndexChanged +=comboBox1_selectedIndexChanged; "comboBox1_selectedIndexChanged" is the selected Index Change event of comboBox1

          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