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.
meteoStationsReferenceTableAdapter.Delete(recordIndex);
myGrid.ItemsSource = null; myGrid.ItemsSource = myDataSource;
<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}"
this.TransactionsDataGrid.ItemsSource = null; this.TransactionsDataGrid.ItemsSource = DataProvider.Instance.GetTransactions();
CollectionViewSource.GetDefaultView(TransactionsDataGrid.ItemsSource).Refresh();
var meteoStationsReferenceDataGrid = this.MeteoStationsReferenceDataGrid; if (meteoStationsReferenceDataGrid?.ItemsSource != null) { var collectionView = CollectionViewSource.GetDefaultView( meteoStationsReferenceDataGrid.ItemsSource); collectionView?.Refresh(); }
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
/* */ meteoStationsReferenceTableAdapter.Delete(recordIndex); /* ClearBeforeFill */ meteoStationsReferenceTableAdapter.ClearBeforeFill = true ; var meteoStationsReferenceDataSet = this._meteoStationsReferenceDataSet ; if ( meteoStationsReferenceDataSet != null ) { /* */ meteoStationsReferenceTableAdapter.Fill ( meteoStationsReferenceDataSet.meteo_stations_reference ) ; }
Source: https://habr.com/ru/post/279011/
All Articles