Delete Data Using LINQ to SQL Class in WPF

Introduction

This article shows you how to delete data using LINQ to SQL in WPF applications.

Create a WPF application as Figure 1.


Figure 1: Create WPF application

Add LINQ to SQL classes to the project as in Figures 2 and 3.

 
Figure 2: Add LINQ to SQL

 
Figure 3: Choose database objects

MainWindow.xaml

  1. <Window x:Class="DeleteData_WPF_LINQtoSQL.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">  
  5.     <Grid>  
  6.         <DataGrid x:Name="dgEmployee"  
  7.                   Width="267"  
  8.                   Margin="118,47,0,0"  
  9.                   HorizontalAlignment="Left"  
  10.                   VerticalAlignment="Top"  
  11.                   AutoGenerateColumns="False"  
  12.                   CanUserDeleteRows="True"  
  13.                   CanUserAddRows="False"  
  14.                   ColumnWidth="*">  
  15.             <DataGrid.Columns>  
  16.                 <DataGridTextColumn x:Name="dgrEmpId"  
  17.                                     Binding="{Binding EmpId}"  
  18.                                     Header="EmpId"  
  19.                                     IsReadOnly="True" />  
  20.                 <DataGridTextColumn x:Name="dgrFirstName"  
  21.                                     Binding="{Binding FirstName}"  
  22.                                     Header="FirstName"  
  23.                                     IsReadOnly="True" />  
  24.                 <DataGridTextColumn x:Name="dgrLastName"  
  25.                                     Binding="{Binding LastName}"  
  26.                                     Header="LastName"  
  27.                                     IsReadOnly="True" />  
  28.                 <DataGridTemplateColumn>  
  29.                     <DataGridTemplateColumn.CellTemplate>  
  30.                         <DataTemplate>  
  31.                             <Button Content="Delete" x:Name="btnDelete" Click="btnDelete_Click"  
  32.                               ></Button>  
  33.                         </DataTemplate>  
  34.                     </DataGridTemplateColumn.CellTemplate>  
  35.                 </DataGridTemplateColumn>  
  36.             </DataGrid.Columns>  
  37.         </DataGrid>  
  38.     </Grid>  
  39. </Window>  
MainWindow.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Forms;  
  8. using System.Windows.Controls;  
  9. using System.Windows.Data;  
  10. using System.Windows.Documents;  
  11. using System.Windows.Input;  
  12. using System.Windows.Media;  
  13. using System.Windows.Media.Imaging;  
  14. using System.Windows.Navigation;  
  15. using System.Windows.Shapes;  
  16.   
  17. namespace DeleteData_WPF_LINQtoSQL  
  18. {  
  19.     /// <summary>  
  20.     /// Interaction logic for MainWindow.xaml  
  21.     /// </summary>  
  22.     public partial class MainWindow : Window  
  23.     {  
  24.         public MainWindow()  
  25.         {  
  26.             InitializeComponent();  
  27.         }  
  28.   
  29.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  30.         {  
  31.             dgEmployee.ItemsSource = objContext.Employees.ToList();  
  32.         }  
  33.   
  34.         DataClasses1DataContext objContext = new DataClasses1DataContext();  
  35.   
  36.         private void btnDelete_Click(object sender, RoutedEventArgs e)  
  37.         {  
  38.             int empId = (dgEmployee.SelectedItem as Employee).EmpId;  
  39.             Employee employee = (from r in objContext.Employees where r.EmpId == empId select r).SingleOrDefault();  
  40.             objContext.Employees.DeleteOnSubmit(employee);  
  41.             objContext.SubmitChanges();  
  42.             dgEmployee.ItemsSource = objContext.Employees.ToList();  
  43.         }  
  44.     }  
  45. }  

The output of the application is as in Figures 4 and 5.

 
Figure 4: Output of the application before applying delete operation

 
Figure 5: Output of the application after applying delete operation.

Summary

In this article we saw how to delete data using LINQ to SQL in WPF application.


MVC Corporation
MVC Corporation is consulting and IT services based company.