刷新WPF Datagrid没有绑定到可观察的集合? [英] Refresh WPF Datagrid not bound to observable collection?

查看:105
本文介绍了刷新WPF Datagrid没有绑定到可观察的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Datagrid的LINQ to SQL,并将数据绑定到自动生成的字段。当通过LINQ提交更改时,数据更新正常。
我遇到的问题是在我直接在数据库上运行SQL语句之后,似乎无法刷新更新的数据。数据库数据正确更新并关闭并重新启动应用程序,数据正确更新。



我已尝试过,

  MyTable.Items.Refresh(); 

  private DatabaseDataContext sql = new DatabaseDataContext(
Properties.Settings.Default.StaffConnectionString);

sql.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

XAML用于将数据绑定到Datagrid列, Info_Data 是数据库表中的自动生成的字段。

 < DataGridTemplateColumn Header =InfoWidth = *SortMemberPath =Words> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock FontWeight =LightText ={Binding Path = Info_Data,UpdateSourceTrigger = PropertyChanged}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>

所有帐户似乎都需要创建一个可观察的集合并将数据绑定并更新该集合来刷新数据网格表。
有什么办法吗?

解决方案

您可以尝试使用SqlDataAdapter和DataTable从数据库加载信息并绑定到DataGrid:

  var da = new SqlDataAdapter(SELECT * FROM+ view,conn); 
var dt = new DataTable();
da.Fill(dt);
var dg = new DataGrid(); //您的DataGrid
dg.ItemsSource = dt.DefaultView;

更新时:

  dt.Rows.Clear(); 
da.Fill(dt);
dt.AcceptChanges();


I am using LINQ to SQL with a Datagrid and binding the data to the auto generated fields. The data updates fine when when submitting changes via LINQ. The problem I am having is I cannot seem to refresh the updated data after I run a SQL statement on the database directly. The Databases data is updated correctly and closing down and re-starting the application the data is updated correctly.

I have tried,

MyTable.Items.Refresh();

And

private DatabaseDataContext sql = new DatabaseDataContext(
            Properties.Settings.Default.StaffConnectionString);

sql.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

The XAML used to bind the data to a Datagrid column, Info_Data is the auto generated field from the Database table.

<DataGridTemplateColumn Header="Info" Width="*" SortMemberPath="Words">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock FontWeight="Light" Text="{Binding Path=Info_Data, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>

By all accounts it seems that I need to create an observable collection and bind the data to that and update the collection to refresh the data-grid table. Is there any way around this?

解决方案

You could try SqlDataAdapter and DataTable to load info from database and bind to DataGrid:

var da = new SqlDataAdapter("SELECT * FROM " + view, conn);
var dt = new DataTable();
da.Fill(dt);
var dg = new DataGrid();// Your DataGrid
dg.ItemsSource = dt.DefaultView;

On update:

dt.Rows.Clear();
da.Fill(dt);
dt.AcceptChanges();

这篇关于刷新WPF Datagrid没有绑定到可观察的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆