Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

2 combo boxes populated with 1 array, remove vaule from combo 2 if its selected in combo1

Posted on 2011-09-08
12
Medium Priority
?
234 Views
Last Modified: 2013-12-17
I have 2 combo boxes on a form for approvals.  I have 1 array loading the names of everyone from 2 sepeate binding sources as you can see in the code below.  Everything works fine, except when I try to remove the item from the second box if its been selected in the first.

string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };
BindingSource source1 = new BindingSource();
BindingSource source2 = new BindingSource();
source1.DataSource = approvers;
cmbFirstApprover.DataSource = source1;
source2.DataSource = approvers;
cmbSecondApprover.DataSource = source2;
        
private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Remove person from second combo
            string firstApprover = cmbFirstApprover.Text;

             //source2.Remove(firstApprover);

             //cmbSecondApprover.Items.Remove(firstApprover);
          }

Open in new window



source2.Remove(firstApprover);
This generats the error below when the approval form loads, even though its in the selectedIndexChanged    
NotSupportedException was unhandled by user code
Collection was of a fixed size  

This generates the error below when I make a selection on the first combo box.
ArgumentException was unhandled
Items collection cannot be modified when the DataSource property is set.
0
Comment
Question by:dustock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36504576
As the error says, you can't remove items when a combo box is bound to a data source. You will have to manually add the items.

string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };

foreach(string approver in approvers)
{
    cmdFirstApprover.Items.Add(new ListItem(approver));
    cmbSecondApprover.Items.Add(new ListItem(approver));
}

Open in new window

0
 
LVL 9

Author Comment

by:dustock
ID: 36504691
strickdd, all this does for me is get rid of the error messages.  It doest remove the item from the second combo box.  My code to display it worked, its my code to remove the item that's not working.

Thanks,

Dustin
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36504748
Did you add back in the code to remove the item from the combo box when the first one is selected?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 9

Author Comment

by:dustock
ID: 36505118
I sure did, it didnt do anything.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36505280
Can you post your code?
0
 
LVL 9

Author Comment

by:dustock
ID: 36505355
I tried both ways to remove again, neither one does anything.

private void TE_IIDB_APPROVAL_Load(object sender, EventArgs e)
{
string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };

foreach (string approver in approvers)
{
     cmbFirstApprover.Items.Add(new ListItem(approver));
     cmbSecondApprover.Items.Add(new ListItem(approver));
}
}

private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
{
     string firstApprover = cmbFirstApprover.Text;
            
      if (cmbFirstApprover.Text != "")
      {
         source2.Remove(firstApprover);
         //cmbSecondApprover.Items.Remove(firstApprover);
       }
}

Open in new window

0
 
LVL 28

Expert Comment

by:strickdd
ID: 36505382
You're no longer using source2 because you are manually binding the information. Just remove the item from the cmbSecondApprover:

private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
{
     string firstApprover = cmbFirstApprover.Text;
            
      if (cmbFirstApprover.Text != "")
      {
         cmbSecondApprover.Items.Remove(firstApprover);
       }
} 

Open in new window

0
 
LVL 9

Author Comment

by:dustock
ID: 36505514
I tried the cmbSecondApprove.Items.Remove, and that didnt work either.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36505570
That is interesting... the other suggestion I have would be this:

string[] approvers = { "Person 1", "Person2", "Parson3", "Person4" };

private void TE_IIDB_APPROVAL_Load(object sender, EventArgs e)
{
foreach (string approver in approvers)
{
     cmbFirstApprover.Items.Add(new ListItem(approver));
     cmbSecondApprover.Items.Add(new ListItem(approver));
}
}

private void cmbFirstApprover_SelectedIndexChanged(object sender, EventArgs e)
{
     string firstApprover = cmbFirstApprover.Text;
            
      if (cmbFirstApprover.Text != "")
      {
           cmdSecondApprover.Items.Clear();

           foreach (string approver in approvers)
{
if(approver != firstApprover)
{
   cmbSecondApprover.Items.Add(new ListItem(approver));
}
}
       }
} 

Open in new window

0
 
LVL 9

Author Comment

by:dustock
ID: 36506008
I'll give that a shot and update you tomorrow.  Thanks
0
 
LVL 9

Accepted Solution

by:
dustock earned 0 total points
ID: 36530917
I switched my code to use List <string> approvers = new List <string>(); instead and I was able to remove the item from the list before I populate the second combobox.
0
 
LVL 9

Author Closing Comment

by:dustock
ID: 36555812
using List <string> I was able to remove the item before populating the second combo box.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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