0
votes

(C#) Recently I am trying to display SQL results from Access MDB to DataGrid upon pressing a button in .xaml. So far I succeed in using ICommand to trigger the SQL and return the results inside a DataTable. I also used PropertyChangedEventHandler to notify the GUI. However, the DataGrid in .xaml does not refresh. (the datagrid appearance remains unchanged)

In the ViewModel, I used below to notify the GUI:

public DataTable TblData
{  
    get { return _dataTable; }
    set
    {
        _dataTable = value;
        RaisePropertyChanged("TblData");
    }
}

In the .xaml.cs, I set the DataContext to my ViewModel:

public MainWindow()
{
    InitializeComponent();
    this.DataContext = _dataViewModel;
}

In the .xaml, I already bind the DataGrid to the DataTable:

<DataGrid Grid.Column="1" Grid.Row="1" 
    ItemsSource="Binding Path=TblData, Mode=OneWay NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged" 
    AutoGenerateColumns="True" />

Any idea on what I should do to fix? (using MVVM)

Or is there any other better way to do it?

Thanks.

1
Does the number and type of columns of the table change over time?Klaus78
No, it just load the static data from MDB upon pressing the button.Tsui John

1 Answers

0
votes

You didnt put brackets on the begining and end of the binding defintion:

<DataGrid Grid.Column="1" Grid.Row="1" 
ItemsSource="{Binding Path=TblData, Mode=OneWay NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
AutoGenerateColumns="True" />