将WPF DataGrid绑定到DataTable [英] Binding a WPF DataGrid to a DataTable
问题描述
我在C#和WPF中是新的,我试图将一个WPF DataGrid绑定到一个DataTable双向。现在,当我编辑DataGrid中的值时,DataTable中的数据会正确更改。当我尝试使用以下代码填写DataTable:
OleDbDataAdapter adapter = new OleDbDataAdapter(a query,(a connection ));
adapter.Fill(dataTable);
代码可以正常工作,而DataGrid似乎没问题。但是当我尝试这样做:
dataTable.Rows [0] [1] =(一些对象);
显示值不变。我尝试以下列方式检查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 =StretchVerticalAlignment =StretchBackground ={TemplateBinding Background}>
< DataGridDetailsPresenter HorizontalAlignment =StretchVerticalAlignment =CenterContent ={TemplateBinding Content}/>
< / Grid>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< Style.Triggers>
<! - triggers - >
< /Style.Triggers>
< / Style>
< /DataGrid.CellStyle>
我已经困扰了这个问题了好几天。感谢任何帮助!
这个工作,也许你可以给一些更多的信息
viewmodel:
public DataTable MyDataTable {get;私人集合}
// 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);
//查看更新为
public void UpdateTable()
{
this.MyDataTable.Rows [0] [1] =haha;
}
xaml
< DataGrid AutoGenerateColumns =TrueItemsSource ={Binding MyDataTable}/>
I'm not good at English because I'm not a native speaker. I apologize if I've made mistakes in the language :)
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);
the code works and the DataGrid seems all right. But when I try this:
dataTable.Rows[0][1] = (some object);
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.
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
viewmodel:
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屋!