如何解决双向数据绑定 dataGrid 和 dataTable
我有一个 DataTable,其中包含来自 sql 数据库的一些数据。我想使用 mvvmcross 将此数据表绑定到数据网格。这是我的代码:
在 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
}
}
在视图中:
<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,UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
使用这些代码,我可以在 DataGrid 中显示 dataTable 的数据,但是当我修改 dataGrid 中的单元格时,dataTable 不会更新(就像我设置了 Mode = OneWay 一样)。
解决方法
问题是通过使用 event handler for the DataTable 解决的,这样当一行改变时,事件处理程序调用更新数据库的方法。
感谢 grek40 的帮助。
另一方面,在 T 是一个类的情况下使用 ObservableCellection
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。