📜 ⬆️ ⬇️

Update DataGrid after DELETE (WPF)

It is given: WPF application for editing the data of a certain table in some database.
Required: make it work as intended (using Visual Studio 2015).
Gag: After clicking the "Delete" button, the grid line remains in place - is not deleted.

A warning


qw1 gave a comment on the material presented:
DataSet is a bad solution for a WPF application.
And in general, this project is architecturally terrible - a lot of code behind, it is better to use pure MVVM.

The data access layer (DAL) must be responsible for loading data from the database.
The data is loaded into an ObservableCollection, which is assigned as an ItemsSource from the DataGrid.
Data manipulation commands call DAL to remove a record from the database and simultaneously remove the item from the collection.

Conclusion: Updating the DataGrid using the method described in the article is impossible, but if you really want it, then you can - just fuck up the application architecture.

Start


I did everything according to the tutorial (" Walkthrough. Binding WPF Controls to a Data Set ").
And everything worked and was good until I added the “DELETE” button.
meteoStationsReferenceTableAdapter.Delete(recordIndex); 

Root of evil


The step-by-step guide didn’t say anything about the “Delete” button, its addition was the cause of all the troubles.
The oddities began:

If you reopen the application, then yes, the record is missing - that is, the record was deleted, but the DataGrid did not draw it.

Finding a solution


I searched the DataGrid in the “Refresh” methods and surprisingly didn’t find anything similar.
I asked Google for a “refresh datagrid wpf c #”.
')
Reset ItemsSource

1) How to refresh datagrid in WPF
 myGrid.ItemsSource = null; myGrid.ItemsSource = myDataSource; 

I did not write the assignment of ItemsSource anywhere, and I was without a clue where this could be - this option was not suitable.
 <DataGrid x:Name="MeteoStationsReferenceDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="0,52,9.6,10" RowDetailsVisibilityMode="VisibleWhenSelected" CanUserReorderColumns="False"> <DataGrid.Columns> <DataGridTextColumn x:Name="IdColumn" Binding="{Binding id}" Header="id" Width="SizeToHeader"/> <DataGridTextColumn x:Name="NameColumn" Binding="{Binding name}" Header="name" Width="SizeToHeader"/> <DataGridTextColumn x:Name="DescriptionColumn" Binding="{Binding description}" Header="description" Width="SizeToHeader"/> </DataGrid.Columns> </DataGrid> 

ItemsSource = "{Binding}"

Any ItemsSource .

Refresh mapping

2) [WPF] DataGrid Update
There are two options, the first:
 this.TransactionsDataGrid.ItemsSource = null; this.TransactionsDataGrid.ItemsSource = DataProvider.Instance.GetTransactions(); 

Where is my DataProvider and what could it be, I was again without a clue.
The second option:
 CollectionViewSource.GetDefaultView(TransactionsDataGrid.ItemsSource).Refresh(); 

It was already clearer, I tried:
solution check
 var meteoStationsReferenceDataGrid = this.MeteoStationsReferenceDataGrid; if (meteoStationsReferenceDataGrid?.ItemsSource != null) { var collectionView = CollectionViewSource.GetDefaultView( meteoStationsReferenceDataGrid.ItemsSource); collectionView?.Refresh(); } 


Nothing has changed - the desired result did not work, ok, we will continue the search.
How much I did not search everywhere was a variant with reset and assignment of ItemsSource . There was also an option with " INotifyPropertyChanged Interface ", but for me it is quite a dense forest.

Decision


I continued my search until Fortune smiled at me by slipping the [RESOLVED] Refresh DataGridView after adding / deleting records link , where it was written in black and white:
 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click '---------------------------------------------------------------------------- Dim newRow = DirectCast(ProduseBindingSource.AddNew(), DataRowView) newRow("pret") = Pret.Text ProduseBindingSource.EndEdit() Me.ProduseTableAdapter.Update(Me.Database1DataSet1) End Sub 

And then it reached me that the data had to be re-read, in the sense of pouring over the new one:
 /*   */ meteoStationsReferenceTableAdapter.Delete(recordIndex); /*  ClearBeforeFill       */ meteoStationsReferenceTableAdapter.ClearBeforeFill = true ; var meteoStationsReferenceDataSet = this._meteoStationsReferenceDataSet ; if ( meteoStationsReferenceDataSet != null ) { /*   */ meteoStationsReferenceTableAdapter.Fill ( meteoStationsReferenceDataSet.meteo_stations_reference ) ; } 

Checked - turned out.
To my taste, re-reading a dataset is a more “civilized” way than resetting and setting ItemsSource .
PS
With WPF, I never became friends.

Sources on GitHub

Source: https://habr.com/ru/post/279011/


All Articles