I am creating a Business Rota App for my college project using WPF and a SQLite database. I wanted to delete a record from a database when clicking on the proportionate ListView item. I tried using 'EmployeeList.SelectedItem' to search for the item on my database which always comes as an error since it's an object. I couldn't find online a method to convert the SelectedItem to the text data stored in the listview which I can use to look for the data on my database using 'db.find' and thus remove it. Am I missing a simple line of code? Thanks
<Window x:Class="Business_Rota_Application.StaffMaintenance"
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:local="clr-namespace:Business_Rota_Application"
mc:Ignorable="d"
Title="StaffMaintenance" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ListView Grid.Column="0" Margin="232,32,232,110" Name="EmployeeList" Grid.RowSpan="3" x:FieldModifier="public">
<ListView.View>
<GridView>
<GridViewColumn Header="EmployeeID" DisplayMemberBinding="{Binding EmployeeID}" Width="100"></GridViewColumn>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" Width="100"></GridViewColumn>
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" Width="100"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Add" HorizontalAlignment="Left" Margin="262,51,0,0" Grid.Row="2" VerticalAlignment="Top" Click="Add_Click" Height="34" Width="76" x:Name="btn_Add"/>
<Button Content="Update" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Top" Height="34" Width="76" Margin="0,51,0,0" x:Name="btn_Update" Click="btn_Update_Click"/>
<Button Content="Remove" HorizontalAlignment="Left" Margin="465,51,0,0" Grid.Row="2" VerticalAlignment="Top" Height="34" Width="76" x:Name="btn_Remove" Click="btn_Remove_Click"/>
<Button Content="Close" HorizontalAlignment="Center" Margin="0,95,0,22" Grid.Row="2" Click="Button_Click"/>
</Grid>
</Window>
private void btn_Remove_Click(object sender, RoutedEventArgs e)
{
try
{
using (AppDBContext db = new AppDBContext())
{
var findEmployee = `db.Employees.Find(EmployeeList.SelectedItems);`
db.Employees.Remove(findEmployee);
db.SaveChanges();
MessageBox.Show("Data successfully Removed.", "Employee App", MessageBoxButton.OK);
EmployeeList.Items.Clear();
PopulateEmployeeData();
}
}
catch (Exception)
{
MessageBox.Show("Error in deleting record", "Employee App", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
CodePudding user response:
Assuming your ListView is bound to a collection of Employee
(if not, change "Employee" to the Type you are using), you can delete all selected items by casting and accessing the EmployeeID
property which is used to find the record.
Unrelated, but you might consider creating a DbContext per Form/Window/Page as per MS recommendation.
try
{
using (AppDBContext db = new AppDBContext())
{
foreach(var emp in EmployeeList.SelectedItems)
{
var empid = ((Employee)emp).EmployeeID;
var findEmployee = db.Employees.Find(empid);
db.Employees.Remove(findEmployee);
}
db.SaveChanges();
MessageBox.Show("Data successfully Removed.", "Employee App", MessageBoxButton.OK);
EmployeeList.Items.Clear();
PopulateEmployeeData();
}
}
catch (Exception)
{
MessageBox.Show("Error in deleting record", "Employee App", MessageBoxButton.OK, MessageBoxImage.Error);
}