DataGrid绑定到Datatable而不刷新值

时间:2021-09-15 00:06:38

I've a DataGrid which is bound to a DataTable ("LastDatasets")

我有一个绑定到DataTable的DataGrid(“LastDatasets”)

When I refresh the DataTable in a Timer via this code:

当我通过以下代码刷新Timer中的DataTable时:

if (LastDatasets != null)
{
    var maxRow = LastDatasets.Select("id = MAX(id)").FirstOrDefault();

    var newds = dataInterface.ReadFromDatabase("SELECT * FROM daten WHERE id > " + dataInterface.SqlSep + "id LIMIT 30", new Dictionary<string, object>() {{"id", maxRow["id"]}});

    if (newds != null && newds.Rows.Count > 0)
    {
        LastDatasets.Merge(newds);
    }               
}

My DataTable contains my new data, but the DataGrid in WPF is not refreshed!

我的DataTable包含我的新数据,但WPF中的DataGrid没有刷新!

4 个解决方案

#1


1  

i use simple binding and i can see all changes after .Merge(). so you should post some more code to see whats going wrong.

我使用简单绑定,我可以看到.Merge()之后的所有更改。所以你应该发布更多的代码来看看出了什么问题。

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

vm.cs

 MyDataTable.Merge(newdata);

#2


0  

After reading new data call

读完新数据后

DataGrid.Refresh();

#3


0  

If you are using binding then use like this

如果您正在使用绑定,请使用这样的方法

ItemsSource="{Binding yourdatasource,IsAsync=True, Mode=TwoWay}"

#4


-1  

The DataTable doesn't implement INotifyPropertyChanged so there isn't a way for your DataGrid to be notified when a property changes. If you were to bind your DataGrid to a collection of object's who raised a PropertyChanged event whenever a property was changed you'd see your values update. I believe your solutions are to do what Ask says and Refresh the contents of your DataGrid or to bind your DataGrid to a collection of objects who implement INotifyPropety Changed instead of a DataTable.

DataTable没有实现INotifyPropertyChanged,因此当属性更改时,无法通知DataGrid。如果您要将DataGrid绑定到一个对象集合,该对象在每次更改属性时都会引发PropertyChanged事件,您会看到您的值更新。我相信您的解决方案是执行Ask所说的内容并刷新DataGrid的内容,或者将DataGrid绑定到实现INotifyPropety Changed而不是DataTable的对象集合。

Another thing to try is to enable virtualization on the DataGrid. By default row virtualization should be enabled but you can double check by setting EnableRowVirtualization = True. Additionally, set EnableColumnVirtualization = true as well.

另一件要尝试的是在DataGrid上启用虚拟化。默认情况下,应启用行虚拟化,但您可以通过设置EnableRowVirtualization = True来进行双重检查。此外,还要设置EnableColumnVirtualization = true。

#1


1  

i use simple binding and i can see all changes after .Merge(). so you should post some more code to see whats going wrong.

我使用简单绑定,我可以看到.Merge()之后的所有更改。所以你应该发布更多的代码来看看出了什么问题。

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

vm.cs

 MyDataTable.Merge(newdata);

#2


0  

After reading new data call

读完新数据后

DataGrid.Refresh();

#3


0  

If you are using binding then use like this

如果您正在使用绑定,请使用这样的方法

ItemsSource="{Binding yourdatasource,IsAsync=True, Mode=TwoWay}"

#4


-1  

The DataTable doesn't implement INotifyPropertyChanged so there isn't a way for your DataGrid to be notified when a property changes. If you were to bind your DataGrid to a collection of object's who raised a PropertyChanged event whenever a property was changed you'd see your values update. I believe your solutions are to do what Ask says and Refresh the contents of your DataGrid or to bind your DataGrid to a collection of objects who implement INotifyPropety Changed instead of a DataTable.

DataTable没有实现INotifyPropertyChanged,因此当属性更改时,无法通知DataGrid。如果您要将DataGrid绑定到一个对象集合,该对象在每次更改属性时都会引发PropertyChanged事件,您会看到您的值更新。我相信您的解决方案是执行Ask所说的内容并刷新DataGrid的内容,或者将DataGrid绑定到实现INotifyPropety Changed而不是DataTable的对象集合。

Another thing to try is to enable virtualization on the DataGrid. By default row virtualization should be enabled but you can double check by setting EnableRowVirtualization = True. Additionally, set EnableColumnVirtualization = true as well.

另一件要尝试的是在DataGrid上启用虚拟化。默认情况下,应启用行虚拟化,但您可以通过设置EnableRowVirtualization = True来进行双重检查。此外,还要设置EnableColumnVirtualization = true。