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. ComboBox selection generating list for another comboBox question

ComboBox selection generating list for another comboBox question

Scheduled Pinned Locked Moved C#
wpfwcfdebugginghelpquestion
24 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.
  • M mprice214

    Almost there.....maybe. I'm having object type issues: I've cleaned things up and have the following:

    private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
    {

            string strPrimary = dataGridView1\[e.ColumnIndex, e.RowIndex\].Value.ToString();
            Debug.WriteLine(strPrimary);
            if (strPrimary == "Test1")
            {
    
                //Build a list 
                var dataSource = new List<Units>();
                dataSource.Add(new Units() { Unit = "blah1" });
                dataSource.Add(new Units() { Unit = "blah2" });
                dataSource.Add(new Units() { Unit = "blah3" });
    
                //Setup data binding 
                this.column2DataGridViewComboBox.DataSource = dataSource;
                this.column2DataGridViewComboBox.DisplayMember = "Unit";
    
            }
        }
    
    
    
        private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cb = e.Control as ComboBox;
    
            if (cb != null)
            {
                // first remove event handler to keep from attaching multiple:
                cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged;
                // now attach the event handler
                cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged;
            }
        }
    

    I get "No overload for 'column1DataGridViewComboBox_SelectedIndexChanged' matches delegate 'System.EventHandler'" However, as you probably know, I can change

    private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)

    to

    private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)

    which fixes that. But I need the DataGridViewCellEventArgs class to access the ColumnIndex and RowIndex properties. Thoughts?

    H Offline
    H Offline
    Henry Minute
    wrote on last edited by
    #14

    For the SelectedIndexChanged handler, you don't need the Column and Row because each time the handler gets called it is called by the relevant ComboBox. Therefore you can get at the ComboBox by Casting the sender parameter. Here we go again. :-D

    private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox cb = sender as ComboBox;
        string someString = cb.SelectedItem;
        // OR
        int cbIndex = cb.SelectedIndex;  // and so on
    }
    

    or, to do it the longer, more difficult to read way

    private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e)
    {
        string someString = ((ComboBox)sender).SelectedItem;
        // OR
        int cbIndex = ((ComboBox)sender).SelectedIndex;  // and so on
    }
    

    Good luck! :)

    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

    M 1 Reply Last reply
    0
    • H Henry Minute

      For the SelectedIndexChanged handler, you don't need the Column and Row because each time the handler gets called it is called by the relevant ComboBox. Therefore you can get at the ComboBox by Casting the sender parameter. Here we go again. :-D

      private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e)
      {
          ComboBox cb = sender as ComboBox;
          string someString = cb.SelectedItem;
          // OR
          int cbIndex = cb.SelectedIndex;  // and so on
      }
      

      or, to do it the longer, more difficult to read way

      private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e)
      {
          string someString = ((ComboBox)sender).SelectedItem;
          // OR
          int cbIndex = ((ComboBox)sender).SelectedIndex;  // and so on
      }
      

      Good luck! :)

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

      M Offline
      M Offline
      mprice214
      wrote on last edited by
      #15

      Wait, let me take my earplugs out....Oh yes, now I think I can see :-D

      private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
      {
      ComboBox cb = e.Control as ComboBox;
      if (cb != null)
      {
      // first remove event handler to keep from attaching multiple:
      cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
      // now attach the event handler
      cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
      }

      Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle, the SelectedIndexChanged event cannot have a handling class that is trying to control the same thing at the same time (in this case the ComboBox). Do you concur?

      H 1 Reply Last reply
      0
      • M mprice214

        Wait, let me take my earplugs out....Oh yes, now I think I can see :-D

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
        ComboBox cb = e.Control as ComboBox;
        if (cb != null)
        {
        // first remove event handler to keep from attaching multiple:
        cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
        // now attach the event handler
        cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
        }

        Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle, the SelectedIndexChanged event cannot have a handling class that is trying to control the same thing at the same time (in this case the ComboBox). Do you concur?

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #16

        mprice214 wrote:

        Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle

        I think that you have slightly misunderstood what is happening here. The EditingControlShowing handler does not pass any arguments, it simply tells the ComboBox where to find the code to execute when its SelectedIndex changes and regardless of whatever else is going on it will fire off that code when the index does change. Of course it may not execute it instantly, not even modern computers can really do more than one thing at a time. It has just occurred to me that you are actually asking about the '-=' and '+=' bits. They are like that because the SelectedIndexChanged contains a list of methods that it should execute. If we used the '+=' line only, then each time we did so another instance of the column1DataGridViewComboBox_SelectedIndexChanged would be added to the list meaning that it would execute once after the first time, twice after the second, three times...... You get the idea. So we first '-=' it to remove the handler from the list, before adding it again. Of course we could test to see if it was there already NOTE** this is not real code

        if (!cb.SelectedIndexChanged.List.Contains(column1DataGridViewComboBox_SelectedIndexChanged))
        {
        cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
        }

        oops forgot to close the tag. but the '-=' then '+=' method is quicker. Hope that this helps. :)

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

        modified on Wednesday, May 19, 2010 10:31 AM

        M 1 Reply Last reply
        0
        • H Henry Minute

          mprice214 wrote:

          Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle

          I think that you have slightly misunderstood what is happening here. The EditingControlShowing handler does not pass any arguments, it simply tells the ComboBox where to find the code to execute when its SelectedIndex changes and regardless of whatever else is going on it will fire off that code when the index does change. Of course it may not execute it instantly, not even modern computers can really do more than one thing at a time. It has just occurred to me that you are actually asking about the '-=' and '+=' bits. They are like that because the SelectedIndexChanged contains a list of methods that it should execute. If we used the '+=' line only, then each time we did so another instance of the column1DataGridViewComboBox_SelectedIndexChanged would be added to the list meaning that it would execute once after the first time, twice after the second, three times...... You get the idea. So we first '-=' it to remove the handler from the list, before adding it again. Of course we could test to see if it was there already NOTE** this is not real code

          if (!cb.SelectedIndexChanged.List.Contains(column1DataGridViewComboBox_SelectedIndexChanged))
          {
          cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
          }

          oops forgot to close the tag. but the '-=' then '+=' method is quicker. Hope that this helps. :)

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

          modified on Wednesday, May 19, 2010 10:31 AM

          M Offline
          M Offline
          mprice214
          wrote on last edited by
          #17

          Henry Minute wrote:

          I think that you have slightly misunderstood what is happening here.

          That's a surprise. :^) Got it, but also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?

          H 1 Reply Last reply
          0
          • M mprice214

            Henry Minute wrote:

            I think that you have slightly misunderstood what is happening here.

            That's a surprise. :^) Got it, but also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #18

            Ah now I understand.

            mprice214 wrote:

            also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?

            No, that is not quite right. The reason is that each event handler has its own, internally defined, set of parameters, that you have to adhere to. Most event handlers have the object sender (in fact I can't think of one off the top of my head that doesn't) after that most also have some form of EventArgs and if the internal definition says it has to have a plain old EventArgs, then it will only work with EventArgs. If the definition says it should have an DataGridViewCellEventArgs then it will only work if you declare it with one of those. SelectedIndexChanged has an EventArgs because it doesn't need to pass any additional information. You know who sent it from the sender parameter. The fact that the event has fired means that the SelectedIndex of the sender has changed and that is all the information you need to make use of it DataGridViewEditingControlShowingEventArgs holds additional information about where in the DataGridView (Column, Row) the event was fired from.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

            M 2 Replies Last reply
            0
            • H Henry Minute

              Ah now I understand.

              mprice214 wrote:

              also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?

              No, that is not quite right. The reason is that each event handler has its own, internally defined, set of parameters, that you have to adhere to. Most event handlers have the object sender (in fact I can't think of one off the top of my head that doesn't) after that most also have some form of EventArgs and if the internal definition says it has to have a plain old EventArgs, then it will only work with EventArgs. If the definition says it should have an DataGridViewCellEventArgs then it will only work if you declare it with one of those. SelectedIndexChanged has an EventArgs because it doesn't need to pass any additional information. You know who sent it from the sender parameter. The fact that the event has fired means that the SelectedIndex of the sender has changed and that is all the information you need to make use of it DataGridViewEditingControlShowingEventArgs holds additional information about where in the DataGridView (Column, Row) the event was fired from.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

              M Offline
              M Offline
              mprice214
              wrote on last edited by
              #19

              I understand. As I continue, I'm sure it will more embedded.

              1 Reply Last reply
              0
              • H Henry Minute

                Ah now I understand.

                mprice214 wrote:

                also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?

                No, that is not quite right. The reason is that each event handler has its own, internally defined, set of parameters, that you have to adhere to. Most event handlers have the object sender (in fact I can't think of one off the top of my head that doesn't) after that most also have some form of EventArgs and if the internal definition says it has to have a plain old EventArgs, then it will only work with EventArgs. If the definition says it should have an DataGridViewCellEventArgs then it will only work if you declare it with one of those. SelectedIndexChanged has an EventArgs because it doesn't need to pass any additional information. You know who sent it from the sender parameter. The fact that the event has fired means that the SelectedIndex of the sender has changed and that is all the information you need to make use of it DataGridViewEditingControlShowingEventArgs holds additional information about where in the DataGridView (Column, Row) the event was fired from.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

                M Offline
                M Offline
                mprice214
                wrote on last edited by
                #20

                Guess who. I have the following:

                private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
                {

                        ComboBox cb = sender as ComboBox;
                
                     
                        string strPrimary = cb.SelectedItem.ToString();
                        Debug.WriteLine(strPrimary);
                
                        var dataSource = new List<Units>();
                
                        switch (strPrimary)
                        {
                            case "1":
                                
                            //Build a list 
                            
                            dataSource.Add(new Units() { Unit = "blah1" });
                            dataSource.Add(new Units() { Unit = "blah2" });
                            dataSource.Add(new Units() { Unit = "blah3" });
                
                            //Setup data binding 
                            this.column2DataGridViewComboBox.DataSource = dataSource;
                            this.column2DataGridViewComboBox.DisplayMember = "Unit";
                
                            break;
                
                            case "2":
                
                            //Build a list 
                            dataSource.Add(new Units() { Unit = "blah4" });
                            dataSource.Add(new Units() { Unit = "blah5" });
                            dataSource.Add(new Units() { Unit = "blah6" });
                
                            //Setup data binding 
                            this.column2DataGridViewComboBox.DataSource = dataSource;
                            this.column2DataGridViewComboBox.DisplayMember = "Unit";
                
                            break;
                
                            case "3":
                
                            //Build a list 
                            dataSource.Add(new Units() { Unit = "blah7" });
                            dataSource.Add(new Units() { Unit = "blah8" });
                            dataSource.Add(new Units() { Unit = "blah9" });
                
                            //Setup data binding 
                            this.column2DataGridViewComboBox.DataSource = dataSource;
                            this.column2DataGridViewComboBox.DisplayMember = "Unit";
                
                            break;
                
                        }
                    }
                

                Well, the problem of course as you know is that ComboBox cb points to the second comboBox once I select it. In fact, once I select a value on the first comboBox and then select the second comboBox to select a value, debug.writeline gives me "WindowsFormsApplication1.Form1+Units" (although the 2nd comboBox does get populated with the appropriate list based upon the selection of the first comboBox). If I use an integer instead of strPrimary and set it as cb.SelectedIndex, what I actually get from debug is the index of the selection of the list of the second comboBox (eg if blah5 is selected, I get an integer = 1) followed by an integer

                H 1 Reply Last reply
                0
                • M mprice214

                  Guess who. I have the following:

                  private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
                  {

                          ComboBox cb = sender as ComboBox;
                  
                       
                          string strPrimary = cb.SelectedItem.ToString();
                          Debug.WriteLine(strPrimary);
                  
                          var dataSource = new List<Units>();
                  
                          switch (strPrimary)
                          {
                              case "1":
                                  
                              //Build a list 
                              
                              dataSource.Add(new Units() { Unit = "blah1" });
                              dataSource.Add(new Units() { Unit = "blah2" });
                              dataSource.Add(new Units() { Unit = "blah3" });
                  
                              //Setup data binding 
                              this.column2DataGridViewComboBox.DataSource = dataSource;
                              this.column2DataGridViewComboBox.DisplayMember = "Unit";
                  
                              break;
                  
                              case "2":
                  
                              //Build a list 
                              dataSource.Add(new Units() { Unit = "blah4" });
                              dataSource.Add(new Units() { Unit = "blah5" });
                              dataSource.Add(new Units() { Unit = "blah6" });
                  
                              //Setup data binding 
                              this.column2DataGridViewComboBox.DataSource = dataSource;
                              this.column2DataGridViewComboBox.DisplayMember = "Unit";
                  
                              break;
                  
                              case "3":
                  
                              //Build a list 
                              dataSource.Add(new Units() { Unit = "blah7" });
                              dataSource.Add(new Units() { Unit = "blah8" });
                              dataSource.Add(new Units() { Unit = "blah9" });
                  
                              //Setup data binding 
                              this.column2DataGridViewComboBox.DataSource = dataSource;
                              this.column2DataGridViewComboBox.DisplayMember = "Unit";
                  
                              break;
                  
                          }
                      }
                  

                  Well, the problem of course as you know is that ComboBox cb points to the second comboBox once I select it. In fact, once I select a value on the first comboBox and then select the second comboBox to select a value, debug.writeline gives me "WindowsFormsApplication1.Form1+Units" (although the 2nd comboBox does get populated with the appropriate list based upon the selection of the first comboBox). If I use an integer instead of strPrimary and set it as cb.SelectedIndex, what I actually get from debug is the index of the selection of the list of the second comboBox (eg if blah5 is selected, I get an integer = 1) followed by an integer

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #21

                  Aaaaand relax! I think I know what is going on here. If you recall, well even if you don't recall actually :) , in my second message to you I had this bit of code: (I've annotated it a bit and modified it v,v slightly)

                  private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                  {
                     ComboBox cb;
                  
                     // switch (dataGrid.CurrentCell.ColumnIndex)   <===================================== silly! already have column in eventargs
                     switch (e.Column)                              //<==============================Use this instead
                     {
                         case 0:  // use the index for your comboboxcolumn    <============ **V IMPORTANT** make sure that the 0: matches the index for your column
                             cb = e.Control as ComboBox;
                             if (cb != null)
                             {
                             // first remove event handler to keep from attaching multiple:
                             cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged;
                             // now attach the event handler
                             cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged;
                             }
                             break;
                          // <================================== You do not need the part below since you only need it to apply to the first cbcolumn
                        //case 3: // use the index for your second combo
                          //   cb = e.Control as ComboBox;
                            // if (cb != null)
                             //{
                             // first remove event handler to keep from attaching multiple:
                           //  cb.SelectedIndexChanged -= cb\_SecondSelectedIndexChanged;
                             // now attach the event handler
                            // cb.SelectedIndexChanged += cb\_SecondSelectedIndexChanged;
                             //}
                             //break;
                     }  
                  }
                  

                  If you can absolutely guarantee that you will only want to do this for one ComboBoxColumn you can simplify the code slightly.

                  private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                  {
                     ComboBox cb;
                  
                     // replace the switch statement by an if statement
                     if (e.Column == 0)  //<============ **V IMPORTANT** change the 0 (if necessary) to  match the index for your column
                     {
                             cb = e.Control as ComboBox;
                             if (cb != null)
                             {
                             // first remove event handler to keep from attaching multiple:
                             cb.Selec
                  
                  M 1 Reply Last reply
                  0
                  • H Henry Minute

                    Aaaaand relax! I think I know what is going on here. If you recall, well even if you don't recall actually :) , in my second message to you I had this bit of code: (I've annotated it a bit and modified it v,v slightly)

                    private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                    {
                       ComboBox cb;
                    
                       // switch (dataGrid.CurrentCell.ColumnIndex)   <===================================== silly! already have column in eventargs
                       switch (e.Column)                              //<==============================Use this instead
                       {
                           case 0:  // use the index for your comboboxcolumn    <============ **V IMPORTANT** make sure that the 0: matches the index for your column
                               cb = e.Control as ComboBox;
                               if (cb != null)
                               {
                               // first remove event handler to keep from attaching multiple:
                               cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged;
                               // now attach the event handler
                               cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged;
                               }
                               break;
                            // <================================== You do not need the part below since you only need it to apply to the first cbcolumn
                          //case 3: // use the index for your second combo
                            //   cb = e.Control as ComboBox;
                              // if (cb != null)
                               //{
                               // first remove event handler to keep from attaching multiple:
                             //  cb.SelectedIndexChanged -= cb\_SecondSelectedIndexChanged;
                               // now attach the event handler
                              // cb.SelectedIndexChanged += cb\_SecondSelectedIndexChanged;
                               //}
                               //break;
                       }  
                    }
                    

                    If you can absolutely guarantee that you will only want to do this for one ComboBoxColumn you can simplify the code slightly.

                    private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                    {
                       ComboBox cb;
                    
                       // replace the switch statement by an if statement
                       if (e.Column == 0)  //<============ **V IMPORTANT** change the 0 (if necessary) to  match the index for your column
                       {
                               cb = e.Control as ComboBox;
                               if (cb != null)
                               {
                               // first remove event handler to keep from attaching multiple:
                               cb.Selec
                    
                    M Offline
                    M Offline
                    mprice214
                    wrote on last edited by
                    #22

                    Good morning (or whatever time of day it is to you). DataGridViewEditingControlShowingEventArgs does not contain the "column" method. I do remember your second message and did try to do that yesterday without success. Let me give you both methods together:

                    private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
                    {

                            ComboBox cb = sender as ComboBox;
                    
                          
                            int intPrimary = cb.SelectedIndex;
                            Debug.WriteLine(intPrimary + "SelectedIndexChanged");
                    
                            var dataSource = new List<Units>();
                    
                            switch (intPrimary)
                            {
                                case 0:
                                    
                                //Build a list 
                                
                                dataSource.Add(new Units() { Unit = "blah1" });
                                dataSource.Add(new Units() { Unit = "blah2" });
                                dataSource.Add(new Units() { Unit = "blah3" });
                    
                                //Setup data binding 
                                this.column2DataGridViewComboBox.DataSource = dataSource;
                                this.column2DataGridViewComboBox.DisplayMember = "Unit";
                    
                                break;
                    
                                case 1:
                    
                                //Build a list 
                                dataSource.Add(new Units() { Unit = "blah4" });
                                dataSource.Add(new Units() { Unit = "blah5" });
                                dataSource.Add(new Units() { Unit = "blah6" });
                    
                                //Setup data binding 
                                this.column2DataGridViewComboBox.DataSource = dataSource;
                                this.column2DataGridViewComboBox.DisplayMember = "Unit";
                    
                                break;
                    
                                case 2:
                    
                                //Build a list 
                                dataSource.Add(new Units() { Unit = "blah7" });
                                dataSource.Add(new Units() { Unit = "blah8" });
                                dataSource.Add(new Units() { Unit = "blah9" });
                    
                                //Setup data binding 
                                this.column2DataGridViewComboBox.DataSource = dataSource;
                                this.column2DataGridViewComboBox.DisplayMember = "Unit";
                    
                                break;
                    
                            }
                        }
                    
                    
                    
                        private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                        {
                            ComboBox cb;
                    
                    
                            Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
                            switch (dataGridView1.CurrentCell.ColumnIndex)
                            {
                                case 0:
                                    
                                cb = e.Contr
                    
                    H 1 Reply Last reply
                    0
                    • M mprice214

                      Good morning (or whatever time of day it is to you). DataGridViewEditingControlShowingEventArgs does not contain the "column" method. I do remember your second message and did try to do that yesterday without success. Let me give you both methods together:

                      private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
                      {

                              ComboBox cb = sender as ComboBox;
                      
                            
                              int intPrimary = cb.SelectedIndex;
                              Debug.WriteLine(intPrimary + "SelectedIndexChanged");
                      
                              var dataSource = new List<Units>();
                      
                              switch (intPrimary)
                              {
                                  case 0:
                                      
                                  //Build a list 
                                  
                                  dataSource.Add(new Units() { Unit = "blah1" });
                                  dataSource.Add(new Units() { Unit = "blah2" });
                                  dataSource.Add(new Units() { Unit = "blah3" });
                      
                                  //Setup data binding 
                                  this.column2DataGridViewComboBox.DataSource = dataSource;
                                  this.column2DataGridViewComboBox.DisplayMember = "Unit";
                      
                                  break;
                      
                                  case 1:
                      
                                  //Build a list 
                                  dataSource.Add(new Units() { Unit = "blah4" });
                                  dataSource.Add(new Units() { Unit = "blah5" });
                                  dataSource.Add(new Units() { Unit = "blah6" });
                      
                                  //Setup data binding 
                                  this.column2DataGridViewComboBox.DataSource = dataSource;
                                  this.column2DataGridViewComboBox.DisplayMember = "Unit";
                      
                                  break;
                      
                                  case 2:
                      
                                  //Build a list 
                                  dataSource.Add(new Units() { Unit = "blah7" });
                                  dataSource.Add(new Units() { Unit = "blah8" });
                                  dataSource.Add(new Units() { Unit = "blah9" });
                      
                                  //Setup data binding 
                                  this.column2DataGridViewComboBox.DataSource = dataSource;
                                  this.column2DataGridViewComboBox.DisplayMember = "Unit";
                      
                                  break;
                      
                              }
                          }
                      
                      
                      
                          private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                          {
                              ComboBox cb;
                      
                      
                              Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
                              switch (dataGridView1.CurrentCell.ColumnIndex)
                              {
                                  case 0:
                                      
                                  cb = e.Contr
                      
                      H Offline
                      H Offline
                      Henry Minute
                      wrote on last edited by
                      #23

                      mprice214 wrote:

                      DataGridViewEditingControlShowingEventArgs does not contain the "column" method.

                      :-D That's what you get for trying to code off the top of your head. Good to see that you were smart enough to use the correct form.:thumbsup: Once again I think I have worked out what is happening. In order to save on system resources, dear old M$ are using the same ComboBox for all ComboBoxCells and just hiding it, rather than destroying it, when not needed. Because it is just hidden, the link to the SelectedIndexChanged never gets deleted, which is why it occurs for column2 as well. After very little thought I see three ways to handle the problem: 1. in the SelectedIndexChanged handler test for the column index before anything else and bomb out (a technical term :) ) if not the one we want.

                      	private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e)
                      	{
                      		if (dataGridView1.CurrentCell.ColumnIndex != 0)   // <=========== Bombing out construct
                      		{
                      			return;
                      		}
                      
                      		ComboBox cb = sender as ComboBox;
                      
                      		int intPrimary = cb.SelectedIndex;
                      		Debug.WriteLine(intPrimary + "SelectedIndexChanged");
                      
                                          ................................
                                          ................................
                                          ................................
                                          ................................
                                   }
                      

                      2. in the EditingControlShowing handler remove the link when we don't find the column we want.

                          private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                          {
                              ComboBox cb;
                      
                      
                              Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
                              switch (dataGridView1.CurrentCell.ColumnIndex)
                              {
                                  case 0:
                                      
                                  cb = e.Control as ComboBox;
                                  if (cb != null)
                                  {
                                      // first remove event handler to keep from attaching multiple:
                                      cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged;
                                      // now attach the event handler
                                      cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged;
                                  }
                                  break;
                                  default:
                      
                      M 1 Reply Last reply
                      0
                      • H Henry Minute

                        mprice214 wrote:

                        DataGridViewEditingControlShowingEventArgs does not contain the "column" method.

                        :-D That's what you get for trying to code off the top of your head. Good to see that you were smart enough to use the correct form.:thumbsup: Once again I think I have worked out what is happening. In order to save on system resources, dear old M$ are using the same ComboBox for all ComboBoxCells and just hiding it, rather than destroying it, when not needed. Because it is just hidden, the link to the SelectedIndexChanged never gets deleted, which is why it occurs for column2 as well. After very little thought I see three ways to handle the problem: 1. in the SelectedIndexChanged handler test for the column index before anything else and bomb out (a technical term :) ) if not the one we want.

                        	private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e)
                        	{
                        		if (dataGridView1.CurrentCell.ColumnIndex != 0)   // <=========== Bombing out construct
                        		{
                        			return;
                        		}
                        
                        		ComboBox cb = sender as ComboBox;
                        
                        		int intPrimary = cb.SelectedIndex;
                        		Debug.WriteLine(intPrimary + "SelectedIndexChanged");
                        
                                            ................................
                                            ................................
                                            ................................
                                            ................................
                                     }
                        

                        2. in the EditingControlShowing handler remove the link when we don't find the column we want.

                            private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                            {
                                ComboBox cb;
                        
                        
                                Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
                                switch (dataGridView1.CurrentCell.ColumnIndex)
                                {
                                    case 0:
                                        
                                    cb = e.Control as ComboBox;
                                    if (cb != null)
                                    {
                                        // first remove event handler to keep from attaching multiple:
                                        cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged;
                                        // now attach the event handler
                                        cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged;
                                    }
                                    break;
                                    default:
                        
                        M Offline
                        M Offline
                        mprice214
                        wrote on last edited by
                        #24

                        Thanks for your help on this. I was able to become much more familiar with the event handlers. I began focusing on DataGridView FAQ Appendix A, 18 that you mentioned a bit ago. I was trying to get that going and was still having some issues and then came across http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/6f6c2632-afd7-4fe9-8bf3-c2c8c08d1a31/[^] Anyway, I'll have to tweak it a bit to keep users from pulling down the second comboBox first (which will throw and exception), but it's exactly what I need. Anyway, you taking the time really helped me out to understand some "basic" things that will help me get through the rest of this project. Thanks again.

                        public partial class Form1 : Form
                        {
                        DataTable tblPrimary, tblSecondary;
                        BindingSource primaryBS, filteredSecondaryBS, unfilteredSecondaryBS;

                            public Form1()
                            {
                        
                                tblPrimary = new DataTable("Primary");
                                tblPrimary.Columns.Add("ID", typeof(int));
                                tblPrimary.Columns.Add("Name", typeof(string));
                        
                                tblSecondary = new DataTable("Secondary");
                                tblSecondary.Columns.Add("ID", typeof(int));
                                tblSecondary.Columns.Add("subID", typeof(int));
                                tblSecondary.Columns.Add("Name", typeof(string));
                        
                                tblPrimary.Rows.Add(new object\[\] { 0, "Force" });
                                tblPrimary.Rows.Add(new object\[\] { 1, "Torque" });
                                tblPrimary.Rows.Add(new object\[\] { 2, "Pressure" });
                        
                                tblSecondary.Rows.Add(new object\[\] { 0, 0, "lb" });
                                tblSecondary.Rows.Add(new object\[\] { 1, 0, "N" });
                                tblSecondary.Rows.Add(new object\[\] { 2, 0, "oz" });
                                tblSecondary.Rows.Add(new object\[\] { 3, 1, "in-lb" });
                                tblSecondary.Rows.Add(new object\[\] { 4, 1, "ft-lb" });
                                tblSecondary.Rows.Add(new object\[\] { 5, 1, "N-m" });
                                tblSecondary.Rows.Add(new object\[\] { 6, 2, "PSI" });
                                tblSecondary.Rows.Add(new object\[\] { 7, 2, "Pa" });
                                tblSecondary.Rows.Add(new object\[\] { 8, 2, "bar" });
                                
                                InitializeComponent();
                        
                                primaryBS = new BindingSource();
                                primaryBS.DataSource = tblPrimary;
                        
                        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