Ali
Ali

Reputation: 29

TwoWay data binding dataGrid and dataTable

I have a DataTable which containes some data from a sql database. I want to bind this dataTable to a dataGrid using mvvmcross. Here are my codes :

In ModelView :

private DataTable _groupeData;
    public DataTable GroupeData
    {
        get { return _groupeData; }
        set
        {
            _groupeData = value;
            RaisePropertyChanged(() => GroupeData);
            Runned++;  //In order to check if the dataTable has updated
            UpdateElementGroupe(EltId, value);  //A method that will update the database using the values in this dataTable
        }

    }

In View :

<DataGrid ItemsSource="{Binding Path=GroupeData}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataGrid.Columns>
                    </DataGrid>

With these codes, I'm able to show dataTable's data in DataGrid, but when I modify a cell in dataGrid, the dataTable doesn't update (as if I had set Mode = OneWay).

Upvotes: 0

Views: 497

Answers (1)

Ali
Ali

Reputation: 29

The problem was solved by using an event handler for the DataTable, so that when a row is changed, the event handler calls the method which updates the database.

Thanks grek40 for your help.

On th other hand, using ObservableCellection<T> where T is a class doesn't solve the problem. But if you are using ObservableCellection, I suppose you can handle the event the same way.

Upvotes: 1

Related Questions