Search code examples
comboboxdatagriditems

Incomplete display of items in combobox wpf c#


I'm using wpf c# and Entity Framework

I have a DataGrid on that show data from database

when users click on datagrid that row will show items in ComboBox (Load on of columns in combobox)

but problem is combobox doesn't show Normal list enter image description here

Code CS Behind :

DENAF1399Entities dbms = new DENAF1399Entities();

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var qre = dbms.Database.SqlQuery<Q_View>("SELECT * FROM Q_View");
    datagrid1.ItemsSource = qre.ToList();
}

private void datagrid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Q_View QVkala = datagrid1.SelectedItem as Q_View;
    if (QVkala != null)
    {
        combobox1.ItemsSource = QVkala.NAMES;
    }
}

I tried

-Change Fonts of combobox

-use new combobox

but didn't work

please help me


Solution

  • Edit during formation: It just became obvious to me what's going on. Q_View.NAMES is a string, and by setting combobox1.ItemsSource to that property, it's identifying the individual items as characters in the string (as string is an IEnumerable<char>).

    If what you want in the combo box is what's in each of the columns of the selected item, then the way to do that is like this:

    private void datagrid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Q_View QVkala = datagrid1.SelectedItem as Q_View;
        if (QVkala != null)
        {
            object[] items = { QVkala.CODE, QVkala.NAME, QVkala.NAMES, QVkala.TOZIH } //etc whatever properties you want to project into this
            combobox1.ItemsSource = items;
        }
    }
    

    ORIGINAL WORK ON AN ANSWER

    At first glance it looks like your data is transposed, but altogether it looks like you aren't using WPF or Entity Framework like you really could be using them. WPF was made for MVVM design and Entity Framework was made for treating tables like collections of objects. Not knowing much else about your application, here's how I'd get started:

    First, I'd move basically everything except what's auto-generated out of MainWindow.xaml.cs, and start a new separate class. (Note: may have compiler errors as this is completely off the cuff)

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged; //MainWindow.xaml will hook into this
        
        public ObservableCollection<Q_View> Q_Views { get; private set; }
        private Q_View selectedQView;
        public Q_View SelectedQView
        {
            get => selectedQView;
            set
            {
                if(value != selectedQView)
                {
                    selectedQView = value;
                    PropertyChanged?.Invoke("SelectedQView");
                }
            }
        }
    }
    

    And then in MainWindow.xaml.cs, the only change from what's generated would be the constructor (there's another way to do this even without changing the code-behind but I'll not get into it here since I'm not as xaml-adept as I am with C#)

    public class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = new MainWindowViewModel();
            InitializeComponent(); //that's auto-generated
        }
    }
    

    And finally, the xaml for your DataGrid. Edit it like this:

    <DataGrid Name="QViewDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Q_Views}" SelectedItem="{Binding SelectedQView}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="CODE" Binding="{Binding Path="CODE"}"> //and so forth with more columns
        </DataGrid.Columns>
    </DataGrid>
    

    ComboBox will have a similar syntax for binding an ItemsSource and SelectedItem. Doing this enables you to avoid having event handlers and dealing with boiler plate for updating so many things.