问题
I have created a databound datagrid in WPF: 'ActionResults', when I run the application it shows the contents of the database table.
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();
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>
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");
}
}
and my button in the MainWindow.xaml.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
}
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?
thank you
EDIT
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:
I'd propose the following, simpler approach:
- Move the code that reads the data from the database to a separate method.
- Add a call to ObservableCollection.Clear at the beginning.
- Call the method at application startup and in the button click event handler.
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();
}
来源:https://stackoverflow.com/questions/21662324/using-inotifypropertychanged-to-refresh-a-databound-datagrid