Solved

Assigning values to a collection of comboboxes

Posted on 2013-05-17
4
163 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.

839 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