简体   繁体   中英

How to refresh my datagrid in c# wpf after use stored procedure in tableadapter

I have set up to use the stored procedure in dataset to insert a new row. The insert code work, but I don't know how to refresh the datagrid for it to show the new data. I setup my datagrid to bind to table like this.

I use the built-in option of visual studio datagrid to make connection. So a lot of part of the code I don't understand.

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

All these code automatically added by Visual Studio if you use their "Data Source"

I found the method to do those. Just use the fucntion fill of table adapter. Can found in cs file of the window. and set the tableAdapter.ClearBeforeFill = true; So every time you fill with default or your own fill function. They will clear the old and fill in the new. Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM