Dante
Dante

Reputation: 3316

Binding DataGrid to DataSet's DataTable

Im working on WPF using MVVM, and I need to bind a DataSet' tables to some DataGrids, but it is not working. My dataSet has 2 tables called "table01" and "table02".

In my XAML I have written the following:

. . . 
<UserControl.DataContext>
    <scr:MyViewModel/>
</UserControl.DataContext>
     <DataGrid Margin="0,0,0,0" Name="myGrid" ItemsSource="{Binding MyDataSet, Path=table01}" />
. . . 

But no data is shown.

In my view model I have written the following:

. . . 
private DataSet myDataSet;
public DataSet MyDataSet
    {
        get
        { 
            return myDataSet;  
        }
        set
        {
            SetAndNotify(ref myDataSet, value, () => MyDataSet);
        }
    }
. . . 

Just to clarify, I fill my dataSet in the view model constructor, even I have printed the content of my dataSet and it has data and I have verified the connection to my DataContext and it is working normally. So, What I am doing wrong??

Hope someone can help me. Thank you in advance.

Upvotes: 0

Views: 852

Answers (2)

blindmeis
blindmeis

Reputation: 22435

if you have an untyped DataSet you can expose your Table as a property like keith suggested or change your binding to

<DataGrid ItemsSource="{Binding MyDataSet.Tables[table01]}" />

Upvotes: 2

keith
keith

Reputation: 3110

Your generic DataSet object won't have a property on it called 'table01', which is what you're trying to bind to.

I suggest perhaps exposing MyDataSet.Tables[0] as it's own property on your ViewModel?

public DataTable MyTable
    {
        get
        { 
            return myDataSet.Tables[0];  
        }
    }

Upvotes: 0

Related Questions