Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Basically, I have a page which has a grid view. However, i want to take a count of all the columns in that grid view and create a drop down list object. so if there are 20 columns in the grid view i want 20 drop down list objects above the grid view.

What I have tried:

Researching on Stack overflow and posted this question there but this was no help.
Posted
Updated 23-Apr-16 4:29am
v2
Comments
CHill60 22-Apr-16 10:36am    
What code have you already tried?
JT1992 22-Apr-16 10:41am    
i tried creating a variable cbox as new combobox() and gave it a width and height however i am unable to get anything
CHill60 22-Apr-16 10:45am    
Well it won't "get anything" unless you populate the comboBox and place it on the Form (or Page - is this ASP.NET?)
What event is going to populate this combobox?
JT1992 22-Apr-16 10:51am    
Sorry i am new to programming and wanted some help. i posted the question above in which the number of comboboxes on the page will be added via the number of columns count.
CHill60 22-Apr-16 10:55am    
Ok. This "page" ... is it a web page (ASP.NET), a WPF page or a WinForm?
Is the grid already on there when it is loaded?
Is the number of columns fixed or is that determined by the databinding?
What do you want to appear in the ComboBoxes?

1 solution

Here is a very simple example. Note that I have a StackPanel (horizontal) to which I am going to add the comboboxes as I create them.
XML
<window x:class="WpfSample.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddControls" Height="314" Width="664" Loaded="Window_Loaded">
    <stackpanel name="SpnlMain" margin="5">
        <datagrid horizontalalignment="Left" height="189" margin="10,0,0,0" width="442" x:name="PresidentsGrid" />
        <stackpanel name="CombosPnl" height="10" orientation="Horizontal" margin="5" />
    </stackpanel>
</window>
And the code
C#
using System.Windows;
using System.Windows.Controls;

namespace WpfSample
{
    public partial class MainWindow 
    {
        public MainWindow()
        {
            InitializeComponent();
            PresidentsGrid.ItemsSource = President.GetPresidents();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var p = new President().GetType().GetProperties();

            for (var i = 0; i < PresidentsGrid.Columns.Count; i++)
            {
                var newCombo = new ComboBox
                {
                    Name = string.Format("Combo{0}", i),
                    Width = 120,
                    ItemsSource = President.GetPresidents(),
                    DisplayMemberPath = p[i].Name,
                    SelectedValuePath = p[i].Name
                };
                CombosPnl.Children.Add(newCombo);
            }
        }
    }
}
This populates a datagrid with a list of US Presidents and then dynamically adds a comboBox for each column in the grid. Each comboBox contains all of the values for the equivalent column. If a new property is added to the class there are no changes to this code required .

Warning - I used reflection to get the properties of my data class into an array so I could easily assign the DisplayMemberPath and SelectedValuePath properties using the loop counter. This only works if the GetPresidents method returns ALL of the class' properties in the order they are defined.

For completeness here is my President class:
C#
using System.Collections.ObjectModel;
namespace WpfSample
{
    class President 
    {
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public int Start { get; set; }
        public int Until { get; set; }
        public int Number { get; set; }

        public President(string fn, string sn, int s, int u, int n)
        {
            FirstName = fn;
            Surname = sn;
            Start = s;
            Until = u;
            Number = n;
        }
        public President() { }

        public static ObservableCollection<president> GetPresidents()
        {
            var sams = new ObservableCollection<president>
            {
                new President("George", "Washington", 1789, 1797,1),
                new President("John", "Adams", 1797, 1801,2),
                new President("Thomas", "Jefferson", 1801, 1809,3),
                new President("James", "Madison", 1809, 1817,4),
                new President("James", "Monroe", 1817, 1825,5),
                new President("John Quincy", "Adams", 1825, 1829,6),
                new President("Andrew", "Jackson", 1829, 1837,7),
                new President("Martin", "Van Buren", 1837, 1841,8),
                new President("William Henry", "Harrison", 1841,1841,9),
                new President("John", "Tyler", 1841, 1845,10)
            };
            return sams;
        }
    }
}</president></president>
 
Share this answer
 
Comments
JT1992 26-Apr-16 14:59pm    
Thank you. would really appreciate if i was given more than 1 star for this post since it was new to me. Thanks.
CHill60 26-Apr-16 15:06pm    
Thank you for accepting my answer. I can't do anything about the 1-star I'm afraid as it wasn't me that did it
JT1992 26-Apr-16 15:08pm    
no problem. that was a very good example of using a loop. Thank you .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900