WPF Datagrid using MVVM.. is two way binding to DataTable possible?

[亡魂溺海] 提交于 2019-12-08 20:24:27

While modifying rows and editing cell contents in the DataTable get reflected in the DataGrid (works for me) you're right that ColumnChanges don't seem to be. If you're using the AutoGenerateColumns option then I imagine it does so at initialization but doesn't watch for changes afterwards.

If you can find an event which fires (I haven't noticed one) when a column is added to the DataTable you could then add it manually in the code behind. Another hack which may or may not be practical would be to set your DataTable propety to null, and then re-set your property to the DataTable, with OnPropertyChanged being called each time. That should force the rebuilding of the DataGrid.

private DataTable _myDataTable;

public DataTable MyDataTable
{
   get { return _myDataTable; }
   set
   {
        _myDataTable = value;
        OnPropertyChanged("MyDataTable");
   }
}

void SomeMethod()
{
     ....results in column changes
     DataTable holder;
     holder = MyDataTable
     MyDataTable = null;
     MyDataTable = holder;         
}
An Van Nguyen

You must use ObservableCollection<> type to binding with DataGrid. Until do this, your DataGrid can update the change of DataTable in ViewModel.

Raising the PropertyChanged event seem useless when you bind with the type rather than ObservableCollection<>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!