绑定一个WPF的DataGrid到DataTable [英] Binding a WPF DataGrid to a DataTable
问题描述
我不擅长英语,因为我不是一个母语。我道歉,如果我做的语言错误:)
I'm not good at English because I'm not a native speaker. I apologize if I've made mistakes in the language :)
我在C#中的新WPF,我想一个WPF DataGrid绑定到双向的DataTable。现在,当我在DataGrid,DataTable中的变化数据正确地编辑值。当我尝试用下面的$ C $三来一补的数据表:
I'm new in C# and WPF and I'm trying to bind a WPF DataGrid to a DataTable in TwoWay. Now when I edit the values in the DataGrid, data in the DataTable change correctly. When I try to fill the DataTable with the following code:
OleDbDataAdapter adapter = new OleDbDataAdapter("a query", (a connection));
adapter.Fill(dataTable);
在code ++工程,并且DataGrid似乎没事了。但是,当我试试这个:
the code works and the DataGrid seems all right. But when I try this:
dataTable.Rows[0][1] = (some object);
显示值不变化。我试着检查下列方式DataGrid中的值:
the display value doesn't change. I try to check the values in the DataGrid in the following way:
MessageBox.Show((SomeDataGrid.Items[0] as DataRowView).Row[1].ToString());
和事实证明没有问题。我不知道为什么显示值都是这样的。
and it turns out no problem. I'm wondering why the display values are like this.
下面是我DataGrid.CellStyle在XAML:
Here's my DataGrid.CellStyle in XAML:
<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>
我一直停留在这个问题上了好几天。感谢您的帮助!
I've been stuck on this problem for several days. Thanks for any help!
推荐答案
这工作,也许你可以给一些更多的信息
this works, maybe you can give some more info
视图模型:
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}"/>
这篇关于绑定一个WPF的DataGrid到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!