使用INotifyPropertyChanged刷新数据绑定的数据网格? [英] Using INotifyPropertyChanged to refresh a databound datagrid?

查看:67
本文介绍了使用INotifyPropertyChanged刷新数据绑定的数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在WPF中创建了一个数据绑定数据网格:"ActionResults",当我运行该应用程序时,它显示了数据库表的内容.

I have created a databound datagrid in WPF: 'ActionResults', when I run the application it shows the contents of the database table.

IT部门.将在SQL Server Management Studio中手动更新"ActionResult"表.如果对该表进行了任何更改,则只有在我重新启动应用程序后,它们才会显示在datagrid中.我想添加一个更新"按钮,它将刷新数据网格,显示对该表所做的任何更改.

The I.T dept. are going to manually update the 'ActionResult' table in SQL server management studio. If any changes are made to the table, they will display in the datagrid only after I restart the application. I would like to add an 'Update' button which will re-fresh the datagrid, displaying any changes made to the table.

我以前试图用类似的东西进行黑客攻击

I was previously trying to do a hack with something like

actionResultsDataGrid.Items.Refresh();

,但决定使用ObservableCollection.因此,我一直在关注 http://www.youtube.com/watch?v=7CKB44Mc4Q0教程的唯一问题是他手动创建了他的记录,我将在其中使用数据库.这是我到目前为止的位置:

but decided to go with the ObservableCollection. So I have been following the http://www.youtube.com/watch?v=7CKB44Mc4Q0 tutorial the only problem is he manually creates his records, where I will be using the db. Here is where I am so far:

//XAML
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WpfApplication2" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="actionResultsViewSource" d:DesignSource="{d:DesignInstance my:ActionResult, CreateList=True}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource actionResultsViewSource}">
        <DataGrid AutoGenerateColumns="True" EnableRowVirtualization="True" ItemsSource="{Binding}" Name="actionResultsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="0,0,0,85">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id}" Header="Id" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="actionColumn" Binding="{Binding Path=Action}" Header="Action" Width="SizeToHeader" />
        </DataGrid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="388,253,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

然后我创建了一个ActionResult类,该类使用INotifyPropertyChanged进行更新.

I then created an ActionResult class which uses the INotifyPropertyChanged to update.

public class ActionResults : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private int _Id;
    public int Id
    {
        get { return _Id; }
        set 
        { 
            _Id = value;
            NotifyPropertyChanged("Id");
        }
    }

    private string _Action;
    public string Action
    {
        get { return _Action; }
        set 
        { 
            _Action = value;
            NotifyPropertyChanged("Action");
        }
    }

和MainWindow.xaml.cs中的我的按钮

and my button in the MainWindow.xaml.cs

private void button1_Click(object sender, RoutedEventArgs e)
{

}

我的问题来自于该教程之后,他然后创建了另一个类'ActionResults_'并手动添加了记录,但是正如我在使用db表一样.我从这里做什么?请告知我被卡住了.可观察的集合在哪里放置?

My question being from following that tutorial he then creates another class 'ActionResults_' and manually adds records, but as I am using the db table. What do I do from here? please advise I am stuck. Where do the observable collections come into place?

谢谢

编辑

   private void Load_ActionResult_Table()
    {
        ActionResultsTable.ActionResultDataSet actionResultDataSet = ((ActionResultsTable.ActionResultDataSet)(this.FindResource("actionResultDataSet")));
        // Load data into the table ActionResult. You can modify this code as needed.
        ActionResultsTable.ActionResultDataSetTableAdapters.ActionResultTableAdapter actionResultDataSetActionResultTableAdapter = new ActionResultsTable.ActionResultDataSetTableAdapters.ActionResultTableAdapter();
        actionResultDataSetActionResultTableAdapter.Fill(actionResultDataSet.ActionResult);
        System.Windows.Data.CollectionViewSource actionResultViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("actionResultViewSource")));
        actionResultViewSource.View.MoveCurrentToFirst();
    }

  private void Refresh_Table_Button_Click(object sender, RoutedEventArgs e)
    {
       //SO DO I NEED TO SAY, SET TABLE TO NULL THEN RELOAD BY USING BELOW FUNCTION?
        Load_ActionResult_Table();
    }

推荐答案

我会提出以下更简单的方法:

I'd propose the following, simpler approach:

  1. 将将从数据库读取数据的代码移至单独的方法.
  2. 在开始时添加对ObservableCollection.Clear的调用.
  3. 在应用程序启动时并在按钮单击事件处理程序中调用该方法.


基于编辑,最简单的方法是在填充数据表之前先清除它:


Based upon the edit, the easiest way would be to clear the DataTable before filling it:

private void Load_ActionResult_Table()
{
    ActionResultsTable.ActionResultDataSet actionResultDataSet = ((ActionResultsTable.ActionResultDataSet)(this.FindResource("actionResultDataSet")));
    // Load data into the table ActionResult. You can modify this code as needed.
    ActionResultsTable.ActionResultDataSetTableAdapters.ActionResultTableAdapter actionResultDataSetActionResultTableAdapter = new ActionResultsTable.ActionResultDataSetTableAdapters.ActionResultTableAdapter();
    // Clear the table
    actionResultDataSet.ActionResult.Clear();
    actionResultDataSetActionResultTableAdapter.Fill(actionResultDataSet.ActionResult);
    System.Windows.Data.CollectionViewSource actionResultViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("actionResultViewSource")));
    actionResultViewSource.View.MoveCurrentToFirst();
}

这篇关于使用INotifyPropertyChanged刷新数据绑定的数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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