private void button2_Click(object sender, EventArgs e) { if (SelectedPlaylist != null) { int rowIndex = DGVPlaylist.CurrentCell.RowIndex; if (rowIndex < 0) { return; } if (MessageBox.Show($"Are you sure want to delete this playlist {SelectedPlaylist.Name}?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DGVPlaylist.Rows.RemoveAt(rowIndex); foreach (var playlistTrack in SelectedPlaylist.PlaylistTracks) { db.Remove(playlistTrack); } db.Remove(SelectedPlaylist); txtPlaylistName.Clear(); db.SaveChanges(); MessageBox.Show($"{SelectedPlaylist.Name} is deleted successfully!", "Info:", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private void button2_Click(object sender, EventArgs e) { if (txtPlaylistName.Text == "") { MessageBox.Show("Please select playlist first!", "Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (_selectedPlaylist != null) { int rowIndex = DGVPlaylist.CurrentCell.RowIndex; if (DGVPlaylist.Rows[rowIndex].Tag is Playlist playlist) { if (MessageBox.Show($"Are you sure want to delete this playlist {playlist.Name}?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DGVPlaylist.Rows.RemoveAt(rowIndex); if (playlist.PlaylistTracks != null) { foreach (var playlistTrack in playlist.PlaylistTracks) { Db.Remove(playlistTrack); } } Db.Playlists?.Remove(playlist); Db.SaveChanges(); AddToTreeView(); txtPlaylistName.Clear(); MessageBox.Show($"{playlist.Name} was deleted successfully!", "Info:", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }