如何在c# wpf中使用tableadapter存储过程后刷新我的数据网格

本文关键字:刷新 存储过程 我的 数据 网格 数据网 tableadapter wpf | 更新日期: 2023-09-27 18:07:20

我已经设置使用数据集中的存储过程插入新行。插入代码工作,但我不知道如何刷新数据网格,以显示新的数据。我设置我的数据网格绑定到表,像这样。

我使用visual studio datagrid的内置选项进行连接。所以很多代码我都看不懂。

XAML

<CollectionViewSource x:Key="studentViewSource" Source="{Binding Student, Source={StaticResource studentDataDataSet}}"/>
<DataGrid x:Name="studentDataGrid" AutoGenerateColumns="False" 
          EnableRowVirtualization="True" ItemsSource="{Binding}"  
          RowDetailsVisibilityMode="VisibleWhenSelected" 
          CanUserAddRows="False" CanUserDeleteRows="False">
</DataGrid>
c#

StudentDatabase.StudentDataDataSet studentDataDataSet = ((StudentDatabase.StudentDataDataSet)(this.FindResource("studentDataDataSet")));
// Load data into the table Student. You can modify this code as needed.
StudentDatabase.StudentDataDataSetTableAdapters.StudentTableAdapter studentDataDataSetStudentTableAdapter = new StudentDatabase.StudentDataDataSetTableAdapters.StudentTableAdapter();
studentDataDataSetStudentTableAdapter.Fill(studentDataDataSet.Student);
System.Windows.Data.CollectionViewSource studentViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("studentViewSource")));
studentViewSource.View.MoveCurrentToFirst();

所有这些代码都是由Visual Studio自动添加的,如果你使用他们的"Data Source"

如何在c# wpf中使用tableadapter存储过程后刷新我的数据网格

我找到了这样做的方法。只需使用表适配器的fill函数即可。可以在窗口的cs文件中找到。并设置tableAdapter。ClearBeforeFill = true;每次你用默认填充或者你自己的填充函数填充。他们将清理旧的,填上新的。例子:

 //Function called when window was load.
 private void window_load(object sender, RoutedEventArgs e)
 {
    studentDataDataSetStudentTableAdapter.ClearBeforeFill = true;
 }
 // Button event called when button press
 private void RefreshDataGrid(object sender, RoutedEventArgs e)
 {
    studentDataDataSetStudentTableAdapter.Fill(studentDataDataSet.Student);
 }