使用INotifyPropertyChanged刷新数据绑定数据网格?

时间:2014-02-09 17:21:50

标签: c# wpf wpfdatagrid observablecollection inotifypropertychanged

我在WPF中创建了一个数据绑定数据网格:'ActionResults',当我运行它时,它会显示数据库表的内容。

I.T部门。将手动更新SQL Server管理工作室中的“ActionResult”表。如果对表进行了任何更改,则只有在重新启动应用程序后,它们才会显示在数据网格中。我想添加一个“更新”按钮,它将重新刷新数据网格,显示对表格所做的任何更改。

我以前试图用

之类的东西进行黑客攻击
actionResultsDataGrid.Items.Refresh();

但决定选择ObservableCollection。所以我一直关注http://www.youtube.com/watch?v=7CKB44Mc4Q0教程唯一的问题是他手动创建他的记录,我将使用db。这是我到目前为止的地方:

//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进行更新。

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中的按钮

private void button1_Click(object sender, RoutedEventArgs e)
{

}

我的问题是从该教程开始,然后他创建另一个类'ActionResults_'并手动添加记录,但是当我使用db表时。我该怎么办?请告诉我卡住了。可观察的藏品在哪里出现?

谢谢

修改

   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();
    }

1 个答案:

答案 0 :(得分:0)

我建议采用以下更简单的方法:

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

  4. 根据编辑,最简单的方法是在填充之前清除DataTable:

    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();
    }