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

x
?
Solved

Assigning values to a collection of comboboxes

Posted on 2013-05-17
4
Medium Priority
?
167 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
[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
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 1000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
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…

721 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