wpf 在数据网格中更改数据时更新数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17237423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update datatable when changed data in the datagrid
提问by Nerielle
I'm new at WPF and for several days I can't find the answer to my question. Please, point me to the useful links...
我是 WPF 的新手,有几天我找不到问题的答案。请指点我有用的链接...
I have a datatable that is bound to the datagrid.
我有一个绑定到数据网格的数据表。
public class Customers:INotifyPropertyChanged
private DataTable _contacts;
public DataTable Contacts
{
get { return _contacts; }
set
{
_contacts = value;
OnPropertyChanged("Contacts");
}
}
public Customers()
{
RaisePropertyChanged("Contacts");
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
if(string.Compare(propertyName,"Contacts")==0)
{
var CustomerContacts = new CustomerContactsTable();
if (Id != null)
CustomerContacts.GetTableByCustomerId(Id);
Contacts = CustomerContacts;
//..here is logic for other properties..
}
}
And here is xaml:
这是xaml:
<DataGrid ItemsSource="{Binding Path=Contacts}" TargetUpdated="dgContacts_TargetUpdated">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name,NotifyOnTargetUpdated=True}" ClipboardContentBinding="{x:Null}" />
<DataGridTextColumn Binding="{Binding TelNo,NotifyOnTargetUpdated=True}" ClipboardContentBinding="{x:Null}" />
</DataGrid.Columns>
</DataGrid>
I don't know how is better to implement the updates of data into database. I want to get two lists of specified datarows: list with updated rows (within new rows inside) and list with deleted rows. I want to work with whole row, not with cells. And I don't use the DataTableAdapter. Any changes in datagrid don't provide the update of sourcecollection now. What event it will be better to use (OnTargetUpdated,RowEditEnding etc.)? Don't beat me so much please. Thanks in advance!
我不知道如何更好地将数据更新到数据库中。我想获得两个指定数据行的列表:包含更新行的列表(在新行内)和包含已删除行的列表。我想处理整行,而不是单元格。而且我不使用 DataTableAdapter。数据网格中的任何更改现在都不提供 sourcecollection 的更新。使用什么事件会更好(OnTargetUpdated、RowEditEnding 等)?请不要打我这么多。提前致谢!
回答by user2509738
You can retrieve modified or deleted rows from the dataset
您可以从数据集中检索修改或删除的行
var modifiedRows = Contacts.GetChanges(DataRowState.Modified);
var deletedRows = Contacts.GetChanges(DataRowState.Deleted);
Consider, as an alternative approach, creating a new ContactViewModel class that implements INotifyPropertyChanged. Use this class as a representation of each row in the datatable.
作为替代方法,请考虑创建一个实现 INotifyPropertyChanged 的新 ContactViewModel 类。使用此类作为数据表中每一行的表示。
回答by Oscar Mateu
I think it's better bind a ObservableCollection instead of a DataTable into a Datagrid to notify inserts or deletes.
我认为最好将 ObservableCollection 而不是 DataTable 绑定到 Datagrid 中以通知插入或删除。
In the link below the solution I adopted to solve a similar problem, although additionally I also needed to update the values of the properties of my binded objects. Hope it helps...
在下面的链接中,我采用了解决类似问题的解决方案,尽管另外我还需要更新绑定对象的属性值。希望能帮助到你...
Datagrid Binding with specialized ObservableCollection that updates item changes
Datagrid 绑定与更新项目更改的专用 ObservableCollection
Oscar
奥斯卡

