Solved

Assigning values to a collection of comboboxes

Posted on 2013-05-17
4
162 Views
Last Modified: 2013-06-05
Hi guys,

I have the following code

                cmbColHeader1.Items.Clear();
                cmbColHeader1.ItemsSource = lst; 

Open in new window


However I have 25 comboboxes, all with the same name and just a different number on the end, there must be a better way than having to do...

                cmbColHeader2.Items.Clear();
                cmbColHeader2.ItemsSource = lst;

                cmbColHeader3.Items.Clear();
                cmbColHeader3.ItemsSource = lst;

                cmbColHeader4.Items.Clear();
                cmbColHeader4.ItemsSource = lst;

etc etc.

Thanks,
Dean
0
Comment
Question by:deanlee17
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 39174312
Loop all combo boxes easily with below code.

foreach (var comboBox in myForm.Controls.OfType<ComboBox>())
{

  if(comboBox.Name.IndexOf("cmbColHeader") != -1)

   {
    comboBox.Items.Clear();
    comboBox.ItemsSource = lst; 

  }
}

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39174324
Looks good but this is a WPF project so myForm throws an error?
0
 

Author Comment

by:deanlee17
ID: 39174705
int Newcount = VisualTreeHelper.GetChildrenCount(gpbColumnHeaders_grid);
                System.Windows.MessageBox.Show(Newcount.ToString());
                for (int i = 0; i < Newcount; i++)
                {
                    Visual childVisual = (Visual)VisualTreeHelper.GetChild(gpbColumnHeaders_grid, i);
                    if (childVisual is System.Windows.Controls.ComboBox)
                    {
                        
                    }
                }

Open in new window


The message box in the above returns the correct number of combo boxes, how do I assign the same data sources to them? I cannot get ..

System.Windows.Controls.ComboBox.ItemsSource

It only allows me to have ItemSourceProperty?

Thanks,
Dean
0
 

Author Comment

by:deanlee17
ID: 39180849
anybody?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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