Solved

Assigning values to a collection of comboboxes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting database in Xamarin? 6 54
Host asp.net pages 5 26
What .NET URL re-routing tool did I use? 2 38
SSRS subreport with parameter 3 9
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

24 Experts available now in Live!

Get 1:1 Help Now