private void LoadDgvServices(bool refresh = false) { int currentCellRow = 0; int currentCellCol = 0; if (dgvServices.CurrentCell != null) { currentCellRow = dgvServices.CurrentCell.RowIndex; currentCellCol = dgvServices.CurrentCell.ColumnIndex; } 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; * }*/ switch (cboServicesView.SelectedIndex) { case 0: dataTable = ProductService.GetAssociatedOfferedActive(new Product(CurrentPlanRecordKeeperProduct.ProductId)); break; case 1: dataTable = ProductService.GetAssociatedOfferedInactive(new Product(CurrentPlanRecordKeeperProduct.ProductId)); break; default: return; } dataTable = dataTable.AsEnumerable().Where(x => x["Type"].ToString() == "Record Keeper").CopyToDataTable(); //dataTable.Columns.Add("ServiceOffered", typeof(bool)); dataTable.Columns.Add("Notes"); 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["ServiceId1"].Visible = false; dgvServices.Columns["ProductId"].Visible = false; dgvServices.Columns["ProductServiceId"].Visible = false; dgvServices.Columns["CreatedBy1"].Visible = false; dgvServices.Columns["CreatedOn1"].Visible = false; dgvServices.Columns["ModifiedBy1"].Visible = false; dgvServices.Columns["ModifiedOn1"].Visible = false; dgvServices.Columns["StateCode1"].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; dgvServices.Columns["Notes"].DisplayIndex = 3; dgvServices.Columns["Notes"].ReadOnly = false; // set service offered values if (refresh == true) { DataTable planRkPdServices = PlanRecordKeeperProductService.GetAssociated(CurrentPlanRecordKeeperProduct); //foreach (DataGridViewRow drServices in dgvServices.Rows) for (int rowIndex = 0; rowIndex < dgvServices.Rows.Count; rowIndex++) { DataGridViewRow drServices = dgvServices.Rows[rowIndex]; Guid serviceId = new Guid(drServices.Cells["ServiceId"].Value.ToString()); var ps = planRkPdServices.AsEnumerable().Where(x => x.Field <Guid>("ServiceId") == serviceId); if (ps.Any()) // rk product already has service record, so update it { DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)drServices.Cells["ServiceOffered"]; var serviceOffered = SqlBoolean.Parse(ps.CopyToDataTable().Rows[0]["ServiceOffered"].ToString()).IsTrue; drServices.Cells["ServiceOffered"].Value = serviceOffered.ToString(); var notes = ps.CopyToDataTable().Rows[0]["Notes"].ToString(); drServices.Cells["Notes"].Value = notes; } } } if (dgvServices.RowCount > 0 && dgvServices.ColumnCount > 0) { DataGridViewCell selectedCell = dgvServices.Rows[currentCellRow].Cells[currentCellCol]; if (selectedCell != null && selectedCell.Visible) { dgvServices.CurrentCell = selectedCell; } else { dgvServices.CurrentCell = dgvServices.FirstDisplayedCell; } } }
private void btnSave_Click(object sender, EventArgs e) { if (cboRecordKeeper.SelectedIndex <= 0) { MessageBox.Show("Error: Record keeper cannot be left blank."); return; } if (cboProduct.SelectedIndex <= 0) { MessageBox.Show("Error: Product cannot be left blank."); return; } else { ListItem li = (ListItem)cboProduct.SelectedItem; Guid productId = (Guid)li.HiddenObject; CurrentPlanRecordKeeperProduct.ProductId = productId; } if (cboPlan.SelectedIndex <= 0) { MessageBox.Show("Error: Plan cannot be left blank."); return; } else { ListItem li = (ListItem)cboPlan.SelectedItem; Plan plan = (Plan)li.HiddenObject; CurrentPlanRecordKeeperProduct.PlanId = plan.PlanId; } if (dateAdded.Checked == false) { CurrentPlanRecordKeeperProduct.DateAdded = null; } else { try { CurrentPlanRecordKeeperProduct.DateAdded = dateAdded.Value; } catch { MessageBox.Show("Error: Date Added string not in date format"); return; } } if (dateRemoved.Checked == false) { CurrentPlanRecordKeeperProduct.DateRemoved = null; } else { try { CurrentPlanRecordKeeperProduct.DateRemoved = dateRemoved.Value; } 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 = PlanRecordKeeperProductService.GetAssociated(CurrentPlanRecordKeeperProduct); 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()); } string notes = dr.Cells["Notes"].Value.ToString(); var ps = productServices.AsEnumerable().Where(x => x.Field <Guid>("ServiceId") == serviceId); PlanRecordKeeperProductService planRkPdService; if (ps.Any()) // rk product already has service record, so update it { Guid planRkPdServiceId = new Guid(ps.CopyToDataTable().Rows[0]["PlanRecordKeeperProductServiceId"].ToString()); planRkPdService = new PlanRecordKeeperProductService(planRkPdServiceId); } else // rk product does not have service record, so create on { planRkPdService = new PlanRecordKeeperProductService(); planRkPdService.ServiceId = serviceId; planRkPdService.PlanRecordKeeperProductId = CurrentPlanRecordKeeperProduct.Id; } planRkPdService.ServiceOffered = serviceOffered; planRkPdService.Notes = notes; planRkPdService.SaveRecordToDatabase(frmMain_Parent.CurrentUser.UserId); } } CurrentPlanRecordKeeperProduct.SaveRecordToDatabase(frmMain_Parent.CurrentUser.UserId); // Do not close form, so users can edit, save, and continue editing //this.Close(); }