0

Warning that I'm relatively new to WPF, EF6 (code first) and MVVM, so I'm counting on this to be a rookie mistake. My views are linked via GalesoftMVVM, using a locator. The xaml file does indeed pick up on things defined in my viewmodel.cs file

I asked a question about this earlier yesterday and got recommended to implement INotifyPropertyChanged, however even with that implemented my datagrid still only shows the headers and no rows unfortunately.

So far this is what I have, any tips or help would be appreciated.

What I would like to do is use the existing database to fill up the collection, and display it on the grid

Viewmodel.cs

public class View1 : ViewModelBase, INotifyPropertyChanged
{
    private ObservableCollection<Table1> _columnCollection = new ObservableCollection<Table1>();
    public ObservableCollection<Table1> ColumnCollection
    {
        get { return this._columnCollection; }
        set { _columnCollection = value; }
    }
}

View1.xaml

<DataGrid Grid.Row="1" Grid.RowSpan="6" x:Name="Datagrid" HeadersVisibility="Column" AutoGenerateColumns="False"

ItemsSource="{Binding Path=ColumnCollection, UpdateSourceTrigger=PropertyChanged}">

<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" />
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>

Datamodel class

public class Table1 : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int _ID;

    public int ID
    {
        get { return _ID; }
        set
        {
            ID = value;
            RaisePropertyChanged("ID");
        }
    }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            RaisePropertyChanged("Name");
        }
    }

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

EFContext class

public class EFContext : DbContext
{
    public EFContext()  : base("name=EFConnectionstring")
    {
    }
    
    public DbSet <Table1> SQLTestTable { get; set; }
}

There are 5 rows of test data in table 1. Headers do show up in the datagrid. (Even when auto-generated columns are turned off & the datatextcolumns are removed, so header lines are definitely within the collection, just not the rows)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I think binding doesn't work because your data context is still ViewModel, and not each element in the collection – Barracuda Apr 16 '21 at 14:31
  • @Barracuda How would I go about adding these to the collection? – polygone321 Apr 16 '21 at 14:33
  • Basic question, but it's not clear from your code: Are you actually adding any `Table1` objects to your `ObservableCollection`? – Chris Mack Apr 16 '21 at 15:22
  • @ChrisMack I dont think so? This is all the code I have for the observablecollection. What would be the best way to go about adding objects(rows)? – polygone321 Apr 16 '21 at 15:27
  • I don't use EF so I'm not too familiar with it, but you need to retrieve a collection of `Table1` objects from your `SQLTestTable` `DbSet` and add them to the collection (e.g., with a `foreach` loop), or perhaps initialise the collection with them, i.e. `new ObservableCollection(someListOfTable1ObjectsThatYouGetFromYourDbSet);`. – Chris Mack Apr 16 '21 at 15:35
  • This might be a good place to start: https://www.learnentityframeworkcore.com/dbset/querying-data. – Chris Mack Apr 16 '21 at 15:38
  • @ChrisMack Will look into it. I think a foreach loop would be the most useful solution, but have no idea how to implemant that. Either way thanks for the link – polygone321 Apr 16 '21 at 16:41
  • Try this example https://stackoverflow.com/questions/5809816/datagrid-binding-in-wpf , which sets the datacontext of the grid to the collection. – Barracuda Apr 16 '21 at 19:21

0 Answers0