2013-07-29 160 views
7

我不擅长英语,因为我不是母语的人。我很抱歉,如果我在语言中犯了错误:)将WPF DataGrid绑定到DataTable

我是新的C#和WPF,我试图将一个WPF DataGrid绑定到TwoWay中的DataTable。现在,当我编辑DataGrid中的值时,DataTable中的数据会正确更改。当我尝试使用以下代码填充DataTable:

OleDbDataAdapter adapter = new OleDbDataAdapter("a query", (a connection)); 
adapter.Fill(dataTable); 

代码工作和DataGrid似乎是所有权利。但是当我试试这个时:

dataTable.Rows[0][1] = (some object); 

显示值不变。我试着检查下列方式DataGrid中的值:

MessageBox.Show((SomeDataGrid.Items[0] as DataRowView).Row[1].ToString()); 

而且事实证明没有问题。我想知道为什么显示值是这样的。

下面是XAML我DataGrid.CellStyle:

<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="DataGridCell"> 
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{TemplateBinding Background}"> 
         <DataGridDetailsPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" Content="{TemplateBinding Content}"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <!-- triggers --> 
     </Style.Triggers> 
    </Style> 
</DataGrid.CellStyle> 

我一直停留在这个问题上了好几天。谢谢你的帮助!

+0

当你定义DataGrid中列,请确保您的UpdateMode属性设置为的PropertyChanged – Sivakumar

回答

0

不幸的是,我不认为DataRow执行INotifyPropertyChangedDataTable执行INotifyCollectionChanged。这些是指示WPF DataBinding在底层源值更改时进行更新的界面。因此,当您更新底层DataTable值时,Binding不会自动刷新,更改将不会反映在DataGrid中。

This link指向一个类似的问题,并提供了一个答案。基本上,如果您希望DataGrid在更改基础数据源中的值时识别并更新,则需要创建一个实现INotifyPropertyChanged的自定义对象/对象。

+1

AFAIK WPF被绑定到强调数据视图,因此到IBindingListView,所以它具有所有通知的东西 – blindmeis

+0

这似乎是一个话题很多辩论,但如果你看看[这个问题]的第二个答案(http:// stackoverflow。com/questions/11154381/datagrid-bound-to-datatable-not-refreshing-values),特别是解释它的注释。 IBindingListView接口不会取代INotifyPropertyChanged的需要。 –

+0

您也可以通过24位MVP查看[此演示文稿](http://es.slideshare.net/EyalV/data-binding-in-depth-presentation),它声明IBindingListView集合中的项目必须实现INotifyPropertyChanged。 –

4

这个工作,也许你可以给一些更多的信息

视图模型:

public DataTable MyDataTable { get; private set; } 

//ctor 
_ds = new DataSet("Test"); 
this.MyDataTable = _ds.Tables.Add("DT"); 
this.MyDataTable.Columns.Add("First"); 
this.MyDataTable.Columns.Add("Second"); 

this.MyDataTable.Rows.Add("11", "12"); 
this.MyDataTable.Rows.Add("21", "22"); 

//view is updated with this 
public void UpdateTable() 
{ 
    this.MyDataTable.Rows[0][1] = "haha"; 
} 

XAML

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding MyDataTable}"/> 
4

我不是很肯定,如果这个解决方案会工作,但它是值得尝试。

而不是将您的DataGrid绑定到DataTable,尝试将其绑定到DataView。您可以使用DefaultView财产转换您DataTableDataView

DataTable dt = new DataTable(); 
DataView dv = dt.DefaultView; 

在有DataView您的通知绑定到数据的变化应该是双向的。

+0

你是正确的dataGrid1.ItemSource = dt.DefaultView; –