populate cascading combobox with Entity Framework

Charles He-MSFT 96 Reputation points Microsoft Employee
2020-03-27T08:16:46.233+00:00

This is a MSDN question asked by zleug, the source is populate cascading combobox with Entity Framework.

Hi All.

I created simple WPF form with two ComboBoxes. I populated one ComboBox

public List<Customer> GetApplication()
        {
            using (CustModel customer = new CustModel())
            {
                List<Customer> cust = customer.Customers.Distinct().ToList();

                cust.Insert(0, new Customer
                {
                    CustomerID = "0",
                    ContactName = "Please select"
                });

                //Assign Entity as DataSource.
                cbCustomer.ItemsSource = cust;
                cbCustomer.DisplayMemberPath = "ContactName";
                cbCustomer.SelectedValue = "CustomerID";
                cbCustomer.Text = "Please select";

                return cust;
            }
        }

How to select in the List only ContactName column and distinct it?

In SQL look like: SELECT distinct [ContactName] FROM Customer

And how populate second ComboBox depended from section from first Combobox?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,185 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,081 Reputation points
    2020-03-27T08:40:05.227+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    The reply form Peter Fleischer

    Xaml:

    <Window x:Class="WpfApp1.Window10"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp10"
            mc:Ignorable="d"
            Title="Window10" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
      <Grid>
        <DataGrid ItemsSource="{Binding View}"/>
      </Grid>
    </Window>
    

    ViewModel:

    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Data;
    
    namespace WpfApp10
    {
      public partial class ViewModel
      {
        public ViewModel()
        {
          cvs.Source= from item in (new WpfApp1.DemoDBEntities()).Tab1.AsEnumerable() select item;
        }
        CollectionViewSource cvs = new CollectionViewSource();
        public ICollectionView View { get => cvs.View; }
      }
    }
    

    In demo I use catalog "Demo" with table "Tab1".

    Thanks.

    No comments