I will like for the user to press the DELETE key and the row should be deleted, unfortunately, although the row dissapears, after I close and re-open the program, the row is still there!!
Here is my code:
public partial class MainWindow : Window
{
NorthWindData.NorthwindEntities context = new NorthWindData.NorthwindEntities();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// dgEmp.ItemsSource = context.Employees.OrderBy(o => o.EmployeeID).ToList();
dgEmp.ItemsSource = GetEmployeeList();
}
private ObservableCollection<Employee> GetEmployeeList()
{
var list = from e in context.Employees select e;
return new ObservableCollection<Employee>(list);
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
context.SaveChanges();
}
private void dgEmp_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Key.Delete == e.Key)
{
foreach (var row in dgEmp.SelectedItems)
{
Employee employee = dgEmp.SelectedItem as Employee;
var deleting = MessageBox.Show("Are you sure you want to delete the employee #" + employee.EmployeeID, "Deleting Record", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
if (deleting == MessageBoxResult.Yes)
{
MessageBox.Show("Deleted Employee {0}" + employee.EmployeeID);
}
else
{
MessageBox.Show("Not Deleted!!");
e.Handled = false;
//dgEmp.Items.Refresh();
}
}
}
}
}Here is my XAML code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:NorthWindData="clr-namespace:NorthWindData;assembly=NorthWindData" mc:Ignorable="d" x:Class="WpfDataGrid.MainWindow"
Title="MainWindow" Height="479.291" Width="908.955" Loaded="Window_Loaded"><Grid><TextBlock Height="55" HorizontalAlignment="Left" Margin="106,29,0,0"
Name="textBlock1" VerticalAlignment="Top" Width="693"
Text="WPF DataGrid for Update and Delete Operations"
TextAlignment="Center" FontFamily="Verdana" FontSize="28" /><DataGrid x:Name="dgEmp" Height="325" Margin="38,89,194,0" VerticalAlignment="Top" CanUserAddRows="True" AutoGenerateColumns="False" CanUserDeleteRows="True" PreviewKeyDown="dgEmp_PreviewKeyDown"><DataGrid.Columns><DataGridTextColumn Binding="{Binding EmployeeID}" Width="100" Header="Employee ID"/><DataGridTextColumn Binding="{Binding LastName}" Width="100" Header="Last Name"/><DataGridTextColumn Binding="{Binding FirstName}" Width="100" Header="First Name"/><DataGridTextColumn Binding="{Binding Title}" Width="150" Header="Title"/><DataGridTemplateColumn Header="Birth Date"><DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding BirthDate, StringFormat=\{0:d\}}" /></DataTemplate></DataGridTemplateColumn.CellTemplate><DataGridTemplateColumn.CellEditingTemplate><DataTemplate><DatePicker SelectedDate ="{Binding BirthDate}" /></DataTemplate></DataGridTemplateColumn.CellEditingTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid><Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" Height="27" Margin="739,106,0,0" VerticalAlignment="Top" Width="136" Click="btnSave_Click"/></Grid></Window>Guisselle