【问题标题】:Binding a WPF DataGrid to a DataTable将 WPF DataGrid 绑定到 DataTable
【发布时间】:2013-07-28 18:13:12
【问题描述】:

我不擅长英语,因为我不是母语人士。如果我在语言上犯了错误,我深表歉意:)

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

标签: c# wpf binding datagrid


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    这行得通,也许你可以提供更多信息

    视图模型:

    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}"/>
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • afaik wpf 绑定到下划线数据视图,因此绑定到 IBindingListView,因此它具有所有通知内容
      • 这似乎是一个备受争议的话题,但如果你看一下this question 的第二个答案,特别是它的 cmets 就会得到解释。 IBindingListView 接口不会取代对 INotifyPropertyChanged 的​​需求。
      • 您也可以通过 MVP 查看this presentation,幻灯片 24,它指出 IBindingListView 集合中的项目必须实现 INotifyPropertyChanged。
      • 试试我的测试代码。它有效,并且没有实施 INotifyPropertyChanged
      猜你喜欢
      • 2018-02-07
      • 2023-04-06
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2011-02-10
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多