//************************************************************************** //*** SAVE AND CANCEL BUTTONS //************************************************************************** // SAVE BUTTON CLICK private void btnSave_Click(object sender, EventArgs e) { // First check if all text boxes and grid view are filled properly if (PackFormValidator.checkTextFields(txtName, txtDesc) && PackFormValidator.checkCommission(txtBasePrice, txtAgComm) && PackFormValidator.checkDates(tpStartDate, tpEndDate) && PackFormValidator.dgvIsNotEmpty(dgvPackProdSuppl) && PackFormValidator.dgvIsFilled(dgvPackProdSuppl)) { // Set this bool variable to true so app won't try to save data twice useSaveButton = true; // Create an instance of Package class from the form Package pack = new Package(); pack.PackName = txtName.Text; pack.PackStartDate = Convert.ToDateTime(tpStartDate.Text); pack.PackEndDate = Convert.ToDateTime(tpEndDate.Text); pack.PackDesc = txtDesc.Text; pack.PackBasePrice = Convert.ToDecimal(txtBasePrice.Text); pack.PackAgncyCommission = Convert.ToDecimal(txtAgComm.Text); pack.PkgImage = imgDescription.Text; // Create a list of innstances of PackIdProdSupId List <PackIdProdSupId> pps = new List <PackIdProdSupId>(); for (int i = 0; i < dgvPackProdSuppl.Rows.Count; i++) { PackIdProdSupId item = new PackIdProdSupId(Convert.ToInt32(txtId.Text), Convert.ToInt32(dgvPackProdSuppl.Rows[i].Cells[4].Value)); pps.Add(item); } // If it is a new form just insert gatherd values into DB if (itIsNewForm == true) { // Insert data into Package Table PackageDB.insertPackages(pack); // Insert data into Packages_Products_Suppliers PackProdSupplierDB.insertPackages_Products_Suppliers(pps); FormHandler.upDatePackList(Convert.ToInt32(txtId.Text)); this.Close(); } else { // Update data in Package Table PackageDB.updatePackages(pack, Convert.ToInt32(txtId.Text)); // Update data in Packages_Products_Suppliers Table PackProdSupplierDB.updatePackages_Products_Suppliers(pps, prodSuppliersIdForUpDate); FormHandler.upDatePackList(Convert.ToInt32(txtId.Text)); this.Close(); } } }
//*******************************************************************************// // END OF CUSTOM USER CONTROL PART //*******************************************************************************// private void PackagesForm_Load(object sender, EventArgs e) { Constants.packageIsChanged = false; if (itIsNewForm == false) { // if form is opened in EDIT mode fill all exusting informartion // Create a DGV table FormHandler.createProdSupplTable(dgvPackProdSuppl); // Create a list of PackProdSuppl objects to popuplate Data Grid View with // existing data List <PackProdSupplier> packProdSupList = PackProdSupplierDB.getProdSuppliersForDGV(Convert.ToInt32(txtId.Text)); // Loop through list generated from data table to populate DataGridView string[] splitLine = new string[5]; foreach (PackProdSupplier line in packProdSupList) { // variable to break list line into array splitLine = line.PackProdSupplierToString().Split(','); // Create a list of possiple values to populate a combox List <Supplier> supplierIdPairs = PackProdSupplier.createSupplierIdPairsList(Convert.ToInt32(splitLine[1])); // Find index of right element int indOfRightElement = supplierIdPairs.IndexOf(supplierIdPairs.Where(p => p.SupplierId == Convert.ToInt32(splitLine[3])).FirstOrDefault()); // Store values from DataTable DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.Cells.Add(new DataGridViewTextBoxCell()); dgvRow.Cells.Add(new DataGridViewTextBoxCell()); dgvRow.Cells.Add(new DataGridViewComboBoxCell()); dgvRow.Cells.Add(new DataGridViewTextBoxCell()); dgvRow.Cells.Add(new DataGridViewTextBoxCell()); dgvRow.Cells[0].Value = splitLine[0]; dgvRow.Cells[1].Value = splitLine[1]; ((DataGridViewComboBoxCell)dgvRow.Cells[2]).FlatStyle = FlatStyle.Flat; ((DataGridViewComboBoxCell)dgvRow.Cells[2]).DataSource = supplierIdPairs; ((DataGridViewComboBoxCell)dgvRow.Cells[2]).DisplayMember = "SupName"; ((DataGridViewComboBoxCell)dgvRow.Cells[2]).ValueMember = "SupplierId"; ((DataGridViewComboBoxCell)dgvRow.Cells[2]).Value = supplierIdPairs[indOfRightElement].SupplierId; dgvRow.Cells[3].Value = splitLine[3]; dgvRow.Cells[4].Value = splitLine[4]; dgvPackProdSuppl.Rows.Add(dgvRow); // add item to prodSuppliersIdForUpDate list prodSuppliersIdForUpDate.Add(Convert.ToInt32(dgvRow.Cells[4].Value)); } } else { // if it is a new form it is needed to generate new Package ID txtId.Text = Convert.ToString((PackageDB.getMaxPackIdValue() + 1)); } //************************************************************************** //****** ADD CUSTOM BUTTON CONTROL //************************************************************************** this.txtbtn = new TextAndButtonControlPackForm(); this.txtbtn.Visible = false; this.dgvPackProdSuppl.Controls.Add(this.txtbtn); //Handle the cellbeginEdit event to show the usercontrol in the cell while editing this.dgvPackProdSuppl.CellBeginEdit += new DataGridViewCellCancelEventHandler(dgv_CellBeginEdit); //Handle the cellEndEdit event to update the cell value this.dgvPackProdSuppl.CellEndEdit += new DataGridViewCellEventHandler(dgv_CellEndEdit); //************************************************************************** //****** END OF CUSTOM BUTTON CONTROL //************************************************************************** FormHandler.captureChanges(this); rtxtHint.SelectionAlignment = HorizontalAlignment.Center; }