我不擅长英语,因为我不是母语的人。我很抱歉,如果我在语言中犯了错误:)将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>
我一直停留在这个问题上了好几天。谢谢你的帮助!
当你定义DataGrid中列,请确保您的UpdateMode属性设置为的PropertyChanged – Sivakumar