git_bsmd/ENI2/Controls/LADG_NST2007Control.xaml.cs

144 lines
4.8 KiB
C#

using bsmd.database;
using ENI2.Excel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace ENI2.Controls
{
/// <summary>
/// Interaction logic for LADG_NST2007Control.xaml
/// </summary>
public partial class LADG_NST2007Control : UserControl
{
private readonly ObservableCollection<LADG_NST2007> _items = new ObservableCollection<LADG_NST2007>();
public LADG_NST2007Control()
{
InitializeComponent();
this.dataGridLADGNST2007.ItemsSource = _items;
var view = CollectionViewSource.GetDefaultView(_items);
view.SortDescriptions.Add(new SortDescription(nameof(LADG_NST2007.NST2007), ListSortDirection.Ascending));
_ = LoadItemsAsync();
this.dataGridLADGNST2007.ContextMenu = new ContextMenu();
MenuItem addItem = new MenuItem
{
Header = Properties.Resources.textAdd,
Icon = new System.Windows.Controls.Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Resources/add.png")) }
};
addItem.Click += AddItem_Click;
this.dataGridLADGNST2007.ContextMenu.Items.Add(addItem);
MenuItem delItem = new MenuItem
{
Header = Properties.Resources.textDelete,
Icon = new System.Windows.Controls.Image { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Resources/delete.png")) }
};
delItem.Click += DelItem_Click;
this.dataGridLADGNST2007.ContextMenu.Items.Add(delItem);
}
private async Task LoadItemsAsync()
{
List<LADG_NST2007> items = await DBManagerAsync.LoadLADGNST2007Async(true);
foreach (LADG_NST2007 item in items)
{
_items.Add(item);
}
}
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
_ = SaveItemsAsync();
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
AddNewItem();
}
private void buttonImport_Click(object sender, RoutedEventArgs e)
{
var imported = ExcelLocalImportHelper.ImportLADGNST2007();
foreach (var item in imported)
{
_items.Add(item);
}
}
private void dataGridLADGNST2007_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Row?.Item is LADG_NST2007 item)
{
item.IsDirty = true;
}
}
private void AddItem_Click(object sender, RoutedEventArgs e)
{
AddNewItem();
}
private async void DelItem_Click(object sender, RoutedEventArgs e)
{
if (this.dataGridLADGNST2007.SelectedItems.Count > 0)
{
if (MessageBox.Show("Are you sure to delete the selected values?", Properties.Resources.textConfirmation, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) ==
MessageBoxResult.Yes)
{
var selectedItems = new List<LADG_NST2007>();
foreach (LADG_NST2007 item in this.dataGridLADGNST2007.SelectedItems)
selectedItems.Add(item);
foreach (LADG_NST2007 item in selectedItems)
{
int result = await DBManagerAsync.DeleteAsync(item);
if (result == 1 || item.IsNew)
{
_items.Remove(item);
}
}
}
}
}
private void AddNewItem()
{
var item = new LADG_NST2007
{
IsDirty = true
};
_items.Add(item);
this.dataGridLADGNST2007.SelectedItem = item;
this.dataGridLADGNST2007.ScrollIntoView(item);
}
private async Task SaveItemsAsync()
{
int totalSaves = 0;
foreach (LADG_NST2007 item in _items)
{
if (item.IsNew || item.IsDirty)
{
if (item.NST2007.IsNullOrEmpty())
continue;
totalSaves += await DBManagerAsync.SaveAsync(item);
}
}
if (totalSaves > 0)
{
MessageBox.Show($"{totalSaves} LADG NST2007 rows saved", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
}