public void dgvload() { dgv_Songs.DataSource = null; dgv_Songs.DataSource = songitems; dgv_Songs.Columns[1].DataPropertyName = "Title"; dgv_Songs.Columns[1].HeaderText = "⮁ " + dgv_Songs.Columns[1].HeaderText; dgv_Songs.Columns[2].DataPropertyName = "Length"; dgv_Songs.Columns[2].HeaderText = "⮁ " + dgv_Songs.Columns[2].HeaderText; dgv_Songs.Columns[3].DataPropertyName = "Album_Name"; dgv_Songs.Columns[3].HeaderText = "⮁" + dgv_Songs.Columns[3].HeaderText; dgv_Songs.Columns[4].DataPropertyName = "Artist_Name"; dgv_Songs.Columns[4].HeaderText = "⮁" + dgv_Songs.Columns[4].HeaderText; dgv_Songs.Columns[5].DataPropertyName = "CreatedAt"; dgv_Songs.Columns[5].HeaderText = "⮁ " + dgv_Songs.Columns[5].HeaderText; dgv_Songs.Columns[6].DataPropertyName = "UpdatedAt"; dgv_Songs.Columns[6].HeaderText = "⮁ " + dgv_Songs.Columns[6].HeaderText; dgv_Songs.Columns[7].DataPropertyName = "Like"; dgv_Songs.Columns[7].HeaderText = "⮁ " + dgv_Songs.Columns[7].HeaderText; dgv_Songs.Columns[8].DataPropertyName = "Delete"; for (int i = dgv_Songs.Columns.Count - 1; i > 8; i--) { dgv_Songs.Columns.RemoveAt(i); } for (int i = 0; i < dgv_Songs.Rows.Count; i++) { dgv_Songs.Rows[i].Cells[0].Value = "▶ Play"; dgv_Songs.Rows[i].Cells[7].Value = "👍 Like"; } using (var ctx = new MusicContext()) { for (int i = 0; i < dgv_Songs.Rows.Count; i++) { SongItem sngitm = (SongItem)dgv_Songs.Rows[i].DataBoundItem; Interaction inter = ctx.Interactions.FirstOrDefault(c => c.User.UserId == Log_In.user.UserId && c.Song.SongId == sngitm.SongId); if (inter != null && inter.Liked == true) { dgv_Songs.Rows[i].Cells[7].Value = "👎 Unlike"; } } } }
private void dgv_Songs_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { var senderGrid = (DataGridView)sender; SongItem sngitm = (SongItem)senderGrid.Rows[e.RowIndex].DataBoundItem;; Song sng = new Song { SongId = sngitm.SongId, Title = sngitm.Title, Length = sngitm.Length, CreatedAt = sngitm.CreatedAt, UpdatedAt = sngitm.UpdatedAt, Album = sngitm.Al, Artist = sngitm.Ar }; if (e.ColumnIndex == 0 || e.ColumnIndex == 7) { using (var ctx = new MusicContext()) { if (senderGrid.CurrentCell.Value == "▶ Play") { if (ctx.Interactions.FirstOrDefault(c => c.User.UserId == Log_In.user.UserId && c.Song.SongId == sng.SongId) == null) { ctx.Interactions.Add(new Interaction { User = ctx.Users.FirstOrDefault(c => c.UserId == Log_In.user.UserId), Song = ctx.Songs.FirstOrDefault(c => c.SongId == sng.SongId), Liked = false, PlayCount = 1, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }); } else { ctx.Interactions.FirstOrDefault(c => c.User.UserId == Log_In.user.UserId && c.Song.SongId == sng.SongId).PlayCount += 1; } for (int i = 0; i < dgv_Songs.Rows.Count; i++) { dgv_Songs.Rows[i].Cells[0].Value = "▶ Play"; } dgv_Songs.CurrentCell.Value = "⏹ Stop"; } else if (senderGrid.CurrentCell.Value == "⏹ Stop") { senderGrid.CurrentCell.Value = "▶ Play"; } else if (senderGrid.CurrentCell.Value == "👍 Like") { if (ctx.Interactions.FirstOrDefault(c => c.User.UserId == Log_In.user.UserId && c.Song.SongId == sng.SongId) == null) { ctx.Interactions.Add(new Interaction { User = ctx.Users.FirstOrDefault(c => c.UserId == Log_In.user.UserId), Song = ctx.Songs.FirstOrDefault(c => c.SongId == sng.SongId), Liked = true, PlayCount = 0, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }); } else { ctx.Interactions.FirstOrDefault(c => c.User.UserId == Log_In.user.UserId && c.Song.SongId == sng.SongId).Liked = true; } senderGrid.CurrentCell.Value = "👎 Unlike"; } else if (senderGrid.CurrentCell.Value == "👎 Unlike") { ctx.Interactions.FirstOrDefault(c => c.User.UserId == Log_In.user.UserId && c.Song.SongId == sng.SongId).Liked = false; senderGrid.CurrentCell.Value = "👍 Like"; } ctx.SaveChanges(); } } else if (e.ColumnIndex == 8) { Delete d = new Delete(); switch (d.ShowDialog()) { case DialogResult.Yes: SongItem snit = (SongItem)dgv_Songs.Rows[e.RowIndex].DataBoundItem; using (var ctx = new MusicContext()) { ctx.Songs.Remove(ctx.Songs.FirstOrDefault(s => s.SongId == snit.SongId)); ctx.SaveChanges(); songs = ctx.Songs.ToList(); songitems = ctx.Songs.Join(ctx.PlaylistSongs, c => c.SongId, x => x.SongId, (c, x) => new { PlaylistId = x.PlaylistId, sngitm = new SongItem { Like = "", Title = c.Title, Length = c.Length, Album_Name = c.Album.Name, Artist_Name = c.Artist.Name, CreatedAt = c.CreatedAt, UpdatedAt = c.UpdatedAt, Delete = "🗑 Delete", AlbumId = c.Album.AlbumId, SongId = c.SongId, Al = c.Album, Ar = c.Artist, ArtistId = c.Artist.ArtistId } }) .Join(ctx.Playlists.Where(u => u.User.UserId == Log_In.user.UserId && u.PlaylistId == ((Playlist)cmb_playlists.SelectedItem).PlaylistId), cx => cx.PlaylistId, p => p.PlaylistId, (cx, p) => cx.sngitm).ToList(); } dgvload(); cmb_Songs.DataSource = null; cmb_Songs.DataSource = songs; cmb_Songs.DisplayMember = "Title"; cmb_Songs.ValueMember = "SongId"; break; case DialogResult.OK: SongItem snit1 = (SongItem)dgv_Songs.Rows[e.RowIndex].DataBoundItem; Playlist pl = (Playlist)cmb_playlists.SelectedItem; using (var ctx = new MusicContext()) { ctx.PlaylistSongs.Remove(ctx.PlaylistSongs.FirstOrDefault(s => s.SongId == snit1.SongId && s.PlaylistId == pl.PlaylistId)); ctx.SaveChanges(); songitems = ctx.Songs.Join(ctx.PlaylistSongs, c => c.SongId, x => x.SongId, (c, x) => new { PlaylistId = x.PlaylistId, sngitm = new SongItem { Like = "", Title = c.Title, Length = c.Length, Album_Name = c.Album.Name, Artist_Name = c.Artist.Name, CreatedAt = c.CreatedAt, UpdatedAt = c.UpdatedAt, Delete = "🗑 Delete", AlbumId = c.Album.AlbumId, SongId = c.SongId, Al = c.Album, Ar = c.Artist, ArtistId = c.Artist.ArtistId } }) .Join(ctx.Playlists.Where(u => u.User.UserId == Log_In.user.UserId && u.PlaylistId == ((Playlist)cmb_playlists.SelectedItem).PlaylistId), cx => cx.PlaylistId, p => p.PlaylistId, (cx, p) => cx.sngitm).ToList(); } dgvload(); break; } } dgv_Songs.Update(); } else { if (lastcolumn != e.ColumnIndex) { ascending = true; } switch (e.ColumnIndex) { case 7: if (ascending) { songitems.Sort((x, y) => x.Like.CompareTo(y.Like)); ascending = false; lastcolumn = 7; } else { songitems.Sort((x, y) => y.Like.CompareTo(x.Like)); ascending = true; } break; case 1: if (ascending) { songitems.Sort((x, y) => x.Title.CompareTo(y.Title)); ascending = false; lastcolumn = 1; } else { songitems.Sort((x, y) => y.Title.CompareTo(x.Title)); ascending = true; } break; case 2: if (ascending) { songitems.Sort((x, y) => x.Length.CompareTo(y.Length)); ascending = false; lastcolumn = 2; } else { songitems.Sort((x, y) => y.Length.CompareTo(x.Length)); ascending = true; } break; case 3: if (ascending) { songitems.Sort((x, y) => x.Album_Name.CompareTo(y.Album_Name)); ascending = false; lastcolumn = 3; } else { songitems.Sort((x, y) => y.Album_Name.CompareTo(x.Album_Name)); ascending = true; } break; case 4: if (ascending) { songitems.Sort((x, y) => x.Artist_Name.CompareTo(y.Artist_Name)); ascending = false; lastcolumn = 4; } else { songitems.Sort((x, y) => y.Artist_Name.CompareTo(x.Artist_Name)); ascending = true; } break; case 5: if (ascending) { songitems.Sort((x, y) => x.CreatedAt.CompareTo(y.CreatedAt)); ascending = false; lastcolumn = 5; } else { songitems.Sort((x, y) => y.CreatedAt.CompareTo(x.CreatedAt)); ascending = true; } break; case 6: if (ascending) { songitems.Sort((x, y) => x.UpdatedAt.CompareTo(y.UpdatedAt)); ascending = false; lastcolumn = 6; } else { songitems.Sort((x, y) => y.UpdatedAt.CompareTo(x.UpdatedAt)); ascending = true; } break; } dgvload(); } }