SortableBindingList<ArtistList> xList = new SortableBindingList<ArtistList>(); xList = (from a in context.Artists orderby a.BirthName select new ArtistList { ArtistId = a.ArtistId, Last_Name = a.BirthName.Split(space).Last(), Age = DateTime.Today.Year - a.DateOfBirth.Year, Song_Count = a.Songs.Count }).ToList(); dgvArtists.DataSource = xList;
SortableBindingList<ArtistList> xList = new SortableBindingList<ArtistList>();
List<ArtistList>
class ArtistList { public int ArtistId { get; set; } public string Last_Name { get; set; } public int Age { get; set; } public int Song_Count { get; set; } }
private void dgvArtists_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (dgvArtists.Columns[e.ColumnIndex].SortMode != DataGridViewColumnSortMode.NotSortable) { if (e.ColumnIndex == newSortColumn) { if (newColumnDirection == ListSortDirection.Ascending) newColumnDirection = ListSortDirection.Descending; else newColumnDirection = ListSortDirection.Ascending; } newSortColumn = e.ColumnIndex; switch (newColumnDirection) { case ListSortDirection.Ascending: dgvArtists.Sort(dgvArtists.Columns[newSortColumn], ListSortDirection.Ascending); break; case ListSortDirection.Descending: dgvArtists.Sort(dgvArtists.Columns[newSortColumn], ListSortDirection.Descending); break; } } } }
IList<ArtistList> xList = new IList<ArtistList>();
class ArtistList { public int ArtistId { get; set; } public string Last_Name { get; set; } public int Age { get; set; } public int Song_Count { get; set; } } List<artistlist> xList = new List<artistlist>(); xList = (from a in context.Artists orderby a.BirthName select new ArtistList { ArtistId = a.ArtistId, Last_Name = a.BirthName.Split(space).Last(), Age = a.DateOfDeath.HasValue ? (Convert.ToDateTime(a.DateOfDeath).Year - a.DateOfBirth.Year) : (DateTime.Today.Year - a.DateOfBirth.Year), Song_Count = a.Songs.Count }).ToList(); dgvArtists.DataSource = xList;