Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

c# - 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...