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. namespace DeleteData_WPF_LINQtoSQL
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. }
  27. private void Window_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. dgEmployee.ItemsSource = objContext.Employees.ToList();
  30. }
  31. DataClasses1DataContext objContext = new DataClasses1DataContext();
  32. private void btnDelete_Click(object sender, RoutedEventArgs e)
  33. {
  34. int empId = (dgEmployee.SelectedItem as Employee).EmpId;
  35. Employee employee = (from r in objContext.Employees where r.EmpId == empId select r).SingleOrDefault();
  36. objContext.Employees.DeleteOnSubmit(employee);
  37. objContext.SubmitChanges();
  38. dgEmployee.ItemsSource = objContext.Employees.ToList();
  39. }
  40. }
  41. }

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.