2013-06-21 103 views
0

我是WPF新手,几天后我找不到问题的答案。请指点我有用的链接...在数据网格中更改数据时更新数据表格

我有一个绑定到数据网格的数据表。

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.. 
      }   

     } 

这里是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> 

我不知道怎么更好地实现数据的更新到数据库中。我想获得两个指定数据行的列表:列出更新的行(在新行内)和删除行列表。我想整行工作,而不是单元格。我不使用DataTableAdapter。 datagrid中的任何更改现在都不提供源集合的更新。 什么事情会更好用(OnTargetUpdated,RowEditEnding等)? 请不要打我这么多。提前致谢!

回答

2

你可以从数据集中

var modifiedRows = Contacts.GetChanges(DataRowState.Modified); 
var deletedRows = Contacts.GetChanges(DataRowState.Deleted); 

考虑修改或删除的行,作为一种替代方法,创建一个新的ContactViewModel类实现INotifyPropertyChanged。使用这个类作为数据表中每一行的表示。

+0

谢谢,'GetChanges'没有返回给我,当我修改datagrid中的数据源集合保持不变时,bcoz。我会尝试为每一行使用特殊类 – Nerielle