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:
Result:
here source code :
o2bnowdf