Why are DataGrid items empty even if it is bound to an ObservableCollection that has rows in WPF?

Mojtaba_Hakim 321 Reputation points
2022-08-27T09:58:17.753+00:00

I'm using C# WPF

I have Datagrid that is filled by an observable collection named: "ALLTASKLIST" filled with Select data from a Database

I used DataBdinging to display data from "ALLTASKLIST" in my DataGrid

but it's empty!

XAML:

   <DataGrid x:Name="MainDatagrid" AutoGenerateColumns="False" ItemsSource="{Binding ALLTASKLIST}">  
            <DataGrid.Columns>  
                <DataGridTextColumn Header="Anbar Code" Width="60" Binding="{Binding CODE, UpdateSourceTrigger=PropertyChanged}" />  
                <DataGridTextColumn Header="Anbar Name" Width="*" Binding="{Binding NAMES, UpdateSourceTrigger=PropertyChanged}"/>  
                  
                <DataGridComboBoxColumn Header="CMB">  
                    <DataGridComboBoxColumn.EditingElementStyle>  
                        <Style TargetType="ComboBox">  
                            <Setter Property="ItemsSource" Value="{Binding Path=ALLTASKLIST}" />  
                            <Setter Property="DisplayMemberPath" Value="NAMES" />  
                            <Setter Property="SelectedValuePath" Value="CODE" />  
                        </Style>  
                    </DataGridComboBoxColumn.EditingElementStyle>  
                </DataGridComboBoxColumn>  
            </DataGrid.Columns>  
  
             
        </DataGrid>  

CS:

 public partial class MainWindow : Window  
    {  
        NEGIN1401Entities dbms = null;  
        public class ThePart1 : INotifyPropertyChanged  
        {  
            private int? _CODE;  
            private string _NAMES;  
            private int? _KIND;  
            private int? _idd;  
            public int? CODE  
            {  
                get { return _CODE; }  
                set  
                {  
                    _CODE = value;  
                    OnPropertyChanged("CODE");  
                }  
            }  
            public string NAMES  
            {  
                get { return _NAMES; }  
                set  
                {  
                    _NAMES = value;  
                    OnPropertyChanged("NAMES");  
                }  
            }  
            public int? KIND  
            {  
                get { return _KIND; }  
                set  
                {  
                    _KIND = value;  
                    OnPropertyChanged("KIND");  
                }  
            }  
            public int? idd  
            {  
                get { return _idd; }  
                set  
                {  
                    _idd = value;  
                    OnPropertyChanged("idd");  
                }  
            }  
            public event PropertyChangedEventHandler PropertyChanged;  
            public void OnPropertyChanged(string strCaller = null)  
            {  
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(strCaller));  
            }  
        }  
        //public ObservableCollection<ThePart1> ALLTASKLIST { get; set; }  
        public ObservableCollection<ThePart1> ALLTASKLIST = new ObservableCollection<ThePart1>();  
        public MainWindow()  
        {  
            InitializeComponent();  
            DataContext = this;  
        }  
        private void Window_Loaded(object sender, RoutedEventArgs e)  
        {  
            dbms = new NEGIN1401Entities(Publications.TheENTTConnectionString);  
  
            ALLTASKLIST = dbms.Database.SqlQuery<ThePart1>("SELECT * FROM TCOD_ANBAR").ToObservableCollection();  
            var IsHaveItems = ALLTASKLIST.Count;//it will be 6 Items  
            //MainDatagrid.ItemsSource = ALLTASKLIST;  
  
        }  

MyDataGrid Items:
235366-have-6-items-in-datagrid.png

Result:

235365-empty-datagrid-result.png

here source code :
o2bnowdf

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-08-27T11:04:29.41+00:00

    Try several adjustments:

    public partial class MainWindow : Window, INotifyPropertyChanged  
    {  
       public event PropertyChangedEventHandler PropertyChanged;  
      
       void NotifyPropertyChanged(string n)  
       {  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs( n ) );  
       }  
      
       public ObservableCollection<ThePart1> ALLTASKLIST { get; set; }  
      
       private void Window_Loaded(object sender, RoutedEventArgs e)  
       {  
          ALLTASKLIST = . . . load data . . .  
      
          NotifyPropertyChanged(nameof(ALLTASKLIST));  
       }  
      
       . . .  
      
    }  
    

    It is not clear what items to use for comboboxes; maybe you should define a separate collection; this issue can be examined separately.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.