将WPF DataGrid绑定到DataTable

时间:2022-01-24 09:17:05

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:

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

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:

代码工作,DataGrid似乎没问题。但是当我尝试这个时:

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:

显示值不会改变。我尝试以下列方式检查DataGrid中的值:

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:

这是我在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>

I've been stuck on this problem for several days. Thanks for any help!

几天来我一直坚持这个问题。谢谢你的帮助!

3 个解决方案

#1


4  

I am not very sure if this solution will work but it is worth a try.

我不太确定这个解决方案是否有效,但值得一试。

Instead of binding your DataGrid to a DataTable, try binding it to a DataView . You can convert your DataTable to a DataView using DefaultView property like

不要将DataGrid绑定到DataTable,而是尝试将其绑定到DataView。您可以使用DefaultView属性将DataTable转换为DataView

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

On binding with DataView your notification to changes in data should work both ways.

在与DataView绑定时,您对数据更改的通知应该是双向的。

#2


4  

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

XAML

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

#3


1  

Unfortunately, I don't believe the DataRow implements INotifyPropertyChanged or the DataTable implements INotifyCollectionChanged. These are the interface that tell a WPF DataBinding to update when the underlying source value changes. So when you update the underlying DataTable values, the Binding is not automatically refreshed and the change won't be reflected in your DataGrid.

不幸的是,我不相信DataRow实现了INotifyPropertyChanged或DataTable实现了INotifyCollectionChanged。这些是告诉WPF DataBinding在基础源值更改时更新的接口。因此,当您更新基础DataTable值时,Binding不会自动刷新,并且更改不会反映在DataGrid中。

This link points to a similar question and provides an answer. Basically, if you want your DataGrid to recognize and update when you change values in the underlying data source, you'll need to create a custom object/objects that implement INotifyPropertyChanged.

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

#1


4  

I am not very sure if this solution will work but it is worth a try.

我不太确定这个解决方案是否有效,但值得一试。

Instead of binding your DataGrid to a DataTable, try binding it to a DataView . You can convert your DataTable to a DataView using DefaultView property like

不要将DataGrid绑定到DataTable,而是尝试将其绑定到DataView。您可以使用DefaultView属性将DataTable转换为DataView

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

On binding with DataView your notification to changes in data should work both ways.

在与DataView绑定时,您对数据更改的通知应该是双向的。

#2


4  

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

XAML

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

#3


1  

Unfortunately, I don't believe the DataRow implements INotifyPropertyChanged or the DataTable implements INotifyCollectionChanged. These are the interface that tell a WPF DataBinding to update when the underlying source value changes. So when you update the underlying DataTable values, the Binding is not automatically refreshed and the change won't be reflected in your DataGrid.

不幸的是,我不相信DataRow实现了INotifyPropertyChanged或DataTable实现了INotifyCollectionChanged。这些是告诉WPF DataBinding在基础源值更改时更新的接口。因此,当您更新基础DataTable值时,Binding不会自动刷新,并且更改不会反映在DataGrid中。

This link points to a similar question and provides an answer. Basically, if you want your DataGrid to recognize and update when you change values in the underlying data source, you'll need to create a custom object/objects that implement INotifyPropertyChanged.

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