private void LoadDgvServices(bool refresh = false) { DataTable dataTable = new DataTable(); /// Set the datatable based on the SelectedIndex of <see cref="cboServicesView"/>. switch (cboServicesView.SelectedIndex) { case 0: dataTable = Service.GetActive(); break; case 1: dataTable = Service.GetInactive(); break; default: return; } dataTable = dataTable.AsEnumerable().Where(x => x["Type"].ToString() == "Record Keeper").CopyToDataTable(); dataTable.Columns.Add("ServiceOffered", typeof(bool)); dgvServices.DataSource = dataTable; // Display/order the columns. dgvServices.Columns["ServiceId"].Visible = false; dgvServices.Columns["Type"].Visible = false; dgvServices.Columns["CreatedBy"].Visible = false; dgvServices.Columns["CreatedOn"].Visible = false; dgvServices.Columns["ModifiedBy"].Visible = false; dgvServices.Columns["ModifiedOn"].Visible = false; dgvServices.Columns["StateCode"].Visible = false; dgvServices.Columns["Name"].DisplayIndex = 0; dgvServices.Columns["Name"].ReadOnly = true; dgvServices.Columns["Category"].DisplayIndex = 1; dgvServices.Columns["Category"].ReadOnly = true; dgvServices.Columns["ServiceOffered"].DisplayIndex = 2; dgvServices.Columns["ServiceOffered"].ReadOnly = false; // set service offered values if (refresh == true) { DataTable planRkServices = PlanRecordKeeperService.GetAssociated(CurrentPlanRecordKeeper); int rowIndex = 0; foreach (DataGridViewRow drServices in dgvServices.Rows) { Guid serviceId = new Guid(drServices.Cells["ServiceId"].Value.ToString()); var ps = planRkServices.AsEnumerable().Where(x => x.Field <Guid>("ServiceId") == serviceId); if (ps.Any()) // rk product already has service record, so update it { DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgvServices.Rows[rowIndex].Cells["ServiceOffered"]; var serviceOffered = SqlBoolean.Parse(ps.CopyToDataTable().Rows[0]["ServiceOffered"].ToString()).IsTrue; dgvServices.Rows[rowIndex].Cells["ServiceOffered"].Value = serviceOffered.ToString(); } rowIndex++; } } }
private void btnSave_Click(object sender, EventArgs e) { if (cboRecordKeeper.SelectedIndex <= 0) { MessageBox.Show("Error: Record keeper cannot be left blank."); return; } else { ListItem li = (ListItem)cboRecordKeeper.SelectedItem; Guid recordKeeperId = (Guid)li.HiddenObject; CurrentPlanRecordKeeper.RecordKeeperId = recordKeeperId; } if (cboPlan.SelectedIndex <= 0) { MessageBox.Show("Error: Plan cannot be left blank."); return; } else { ListItem li = (ListItem)cboPlan.SelectedItem; Plan plan = (Plan)li.HiddenObject; CurrentPlanRecordKeeper.PlanId = plan.PlanId; } if (String.IsNullOrWhiteSpace(txtDateAdded.Text)) { CurrentPlanRecordKeeper.DateAdded = null; } else { try { CurrentPlanRecordKeeper.DateAdded = DateTime.Parse(txtDateAdded.Text); } catch { MessageBox.Show("Error: Date Added string not in date format"); return; } } if (String.IsNullOrWhiteSpace(txtDateRemoved.Text)) { CurrentPlanRecordKeeper.DateRemoved = null; } else { try { CurrentPlanRecordKeeper.DateRemoved = DateTime.Parse(txtDateRemoved.Text); } catch { MessageBox.Show("Error: Date Removed string not in date format"); return; } } // loop through dgvservices, and update productservice records for record keeper product if (dgvServices.Rows.Count > 0) { DataTable productServices = PlanRecordKeeperService.GetAssociated(CurrentPlanRecordKeeper); foreach (DataGridViewRow dr in dgvServices.Rows) { Guid serviceId = new Guid(dr.Cells["ServiceId"].Value.ToString()); bool serviceOffered = false; if (dr.Cells["ServiceOffered"].Value.ToString() != "") { serviceOffered = bool.Parse(dr.Cells["ServiceOffered"].Value.ToString()); } var ps = productServices.AsEnumerable().Where(x => x.Field <Guid>("ServiceId") == serviceId); if (ps.Any()) // rk product already has service record, so update it { Guid planRkServiceId = new Guid(ps.CopyToDataTable().Rows[0]["PlanRecordKeeperServiceId"].ToString()); PlanRecordKeeperService planRkService = new PlanRecordKeeperService(planRkServiceId); planRkService.ServiceOffered = serviceOffered; planRkService.SaveRecordToDatabase(frmMain_Parent.CurrentUser.UserId); } else // rk product does not have service record, so create on { PlanRecordKeeperService planRkService = new PlanRecordKeeperService(); planRkService.ServiceId = serviceId; planRkService.PlanRecordKeeperId = CurrentPlanRecordKeeper.Id; planRkService.ServiceOffered = serviceOffered; planRkService.SaveRecordToDatabase(frmMain_Parent.CurrentUser.UserId); } } } CurrentPlanRecordKeeper.SaveRecordToDatabase(frmMain_Parent.CurrentUser.UserId); this.Close(); }