private void btnReport_Click(object sender, EventArgs e) { if (profileDataGridView.SelectedRows.Count > 0) { int[] profileIds = new int[profileDataGridView.SelectedRows.Count]; int i = 0; foreach (DataGridViewRow row in profileDataGridView.SelectedRows) { profileIds[i] = Convert.ToInt32(row.Cells["Id"].Value); i++; } DataTable profiles = QueryDb.SelectProfilesByIds(profileIds); DataTable recentJobs = QueryDb.SelectJobsByProfileIds(profileIds); List <ProfileJobs> profJobList = new List <ProfileJobs>(); foreach (var profile in profiles.AsEnumerable()) { ProfileJobs pj = new ProfileJobs(); pj.Profile = new Profile(profile); DataTable jobs = recentJobs.AsEnumerable() .Where(r => r.Field <int>("ProfileId") == pj.Profile.Id).CopyToDataTable(); foreach (var job in jobs.AsEnumerable()) { RecentJob rj = new RecentJob(job); pj.AddRecentJob(rj); } profJobList.Add(pj); } ReportForm report = new ReportForm(profJobList); report.Show(); } }