Tomáš Zajda
Tomáš Zajda

Reputation: 147

DataGrid binding not working with DataView

I'm trying to show my database in a DataGrid in WPF, but I just can't find a way to make it work. I'm using DataTable and DataView to get the data into the DataGrid, but it always ends up with XamlParseException even though the DataView is filled with the info from the database. I am using Visual Studio 2015.

Anyone has a clue about where the problem could be?

This is part of my WPF with the DataGrid (only with the id parameter for now):

`    
xmlns:vm="clr-namespace:FilmovaDatabaze.ViewModels"
<Window.Resources>
        <vm:MainVm x:Key="MainVm"/>
</Window.Resources>

<DataGrid ItemsSource="{Binding Source={StaticResource MainVm}, Path=DataView}">
            <DataGridTextColumn Header="ID" Binding="{Binding id}"/>
        </DataGrid>`

And this is part of the code in ViewModel:

private DataView _dataView;
private DataTable _data;

public DataView DataView { get { return _dataView; } set { _dataView = value; this.ChangeProperty("DataView"); } }

public MainVM(){
            _enabledChanges = true;
            _data = new DataTable();
            _data = Db.GetTable("SELECT * FROM movies JOIN directors ON directors.id = movies.director_id", "id ASC");
            _dataView = new DataView(_data);}

Upvotes: 0

Views: 1294

Answers (1)

Tom&#225;š Zajda
Tom&#225;š Zajda

Reputation: 147

Finally I've found the solution. There is a missing layer in the XAML. It has to look like this:

<DataGrid ItemsSource="{Binding Source={StaticResource MainVm}, Path=DataView}">
<DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding id}"/>
</Datagrid.Columns>
        </DataGrid>

Upvotes: 1

Related Questions