Solved

Assigning values to a collection of comboboxes

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now