none
Populate ComboBox with the columns of an entity RRS feed

  • Question

  • In my application WPF/Entity Framework I need to populate a ComboBox control with the columns of an entity. So I solved by writing the following code:

    private void Button_Click(object sender, RoutedEventArgs e)
            {
                using (var ctx = new MyContext())
                {
                    var colNames = typeof(Customer).GetProperties().Select(a => a.Name).ToList();
                    cBox.ItemsSource = colNames;
                }
            }
    However, my problem is to make some columns are excluded, such as: "ID", "Name" and "Surname". How to do it?

    Saturday, April 11, 2015 9:08 AM

Answers

  • You could use the Where method to exclude certain properties:

    private void Button_Click(object sender, RoutedEventArgs e)
            {
                using (var ctx = new MyContext())
                {
                    var colNames = typeof(Customer).GetProperties().Select(a => a.Name).Where(a => !a.Equals("ID") && !a.Equals("Name") && !a.Equals("Surname")).ToList();
                    cBox.ItemsSource = colNames;
                }
            }

    Please remember to close your threads by marking helpful posts as answer and then please start a new thread if you have a new question.


    • Edited by Magnus (MM8)MVP Saturday, April 11, 2015 9:23 AM
    • Marked as answer by piedatt80 Saturday, April 11, 2015 3:21 PM
    Saturday, April 11, 2015 9:22 AM

All replies

  • You could use the Where method to exclude certain properties:

    private void Button_Click(object sender, RoutedEventArgs e)
            {
                using (var ctx = new MyContext())
                {
                    var colNames = typeof(Customer).GetProperties().Select(a => a.Name).Where(a => !a.Equals("ID") && !a.Equals("Name") && !a.Equals("Surname")).ToList();
                    cBox.ItemsSource = colNames;
                }
            }

    Please remember to close your threads by marking helpful posts as answer and then please start a new thread if you have a new question.


    • Edited by Magnus (MM8)MVP Saturday, April 11, 2015 9:23 AM
    • Marked as answer by piedatt80 Saturday, April 11, 2015 3:21 PM
    Saturday, April 11, 2015 9:22 AM
  • OK, I admit that this is already a valid solution!

    But considering that a project can contain many tables,I had thought of something more generic, although a little more complex.
    That is, treat colNames as a string and create columns with certain special characters such as the column"Name" and "Surname" as "NameDel" and"SurnameDel" then
    delete from the list those columns that contain the characters "Del".

    Saturday, April 11, 2015 10:47 AM
  • >>OK, I admit that this is already a valid solution!

    Your original question was about how to exclude certain properties and now you are asking something completely different.

    Please close your thread by marking helpful posts as answer once the original question has been answered and then start a new thread if you have a new question.

    >>That is, treat colNames as a string and create columns with certain special characters such as the column"Name" and "Surname" as "NameDel" and"SurnameDel" then delete from the list those columns that contain the characters "Del".

    Property names are not generic. But you can of course do whatever you want with the column names and add only the ones you want and create new ones as you wish, e.g:

                var colNames = typeof(Customer).GetProperties().Select(a => a.Name).ToList();
                List<string> namesToUse = new List<string>();
                foreach (string col in colNames)
                {
                    string name = col;
                    if (col == "Name" || col == "Surname")
                    {
                        name += "Del";
                    }
                }
                cBox.ItemsSource = namesToUse;

    But once again, you did mention nothing about this requirement originally so please close the thread by marking all helpful posts as answer and then start a new one if you have another issue.

    Saturday, April 11, 2015 11:23 AM