[英]How to refresh my datagrid in c# wpf after use stored procedure in tableadapter
我已經設置為使用數據集中的存儲過程來插入新行。 插入代碼可以工作,但是我不知道如何刷新數據網格以顯示新數據。 我將我的數據網格設置為像這樣綁定到表。
我使用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會自動添加所有這些代碼
我找到了做這些的方法。 只需使用表適配器的功能填充即可。 可以在窗口的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.