private void btnInsert_Click(object sender, EventArgs e) { //index row đang được chọn int rowindex = dataGridViewProduct.CurrentCell.RowIndex; // lấy id string id = dataGridViewProduct.Rows[rowindex].Cells[0].Value.ToString(); string name = dataGridViewProduct.Rows[rowindex].Cells[1].Value.ToString(); //int gia = int.Parse(dataGridViewProduct.Rows[rowindex].Cells[2].Value.ToString()); using (AddProductToInvoiceConfirmForm confirmForm = new AddProductToInvoiceConfirmForm(dataGridViewProduct.Rows[rowindex].Cells[2].Value.ToString())) { if (confirmForm.ShowDialog() == DialogResult.OK) { int soLuong = confirmForm.SoLuong; int gia = confirmForm.GiaGoc; // kiểm tra xem item này đã có bên gridview bên phải chưa // có rồi thì +số lượng int rowFound = -1; foreach (DataGridViewRow gridrow in dataGridViewForm.Rows) { if (gridrow.Cells[0].Value != null && gridrow.Cells[0].Value.ToString().Equals(id)) { rowFound = gridrow.Index; dataGridViewForm.Rows[dataGridViewForm.SelectedRows[0].Index].Selected = false; dataGridViewForm.Rows[rowFound].Selected = true; int val; int.TryParse(dataGridViewForm.Rows[rowFound].Cells[2].Value.ToString(), out val); if (val > 0) { val += soLuong; dataGridViewForm.Rows[rowFound].Cells[2].Value = val.ToString(); } break; } } if (rowFound == -1) { DataGridViewRow row = (DataGridViewRow)dataGridViewForm.RowTemplate.Clone(); row.CreateCells(dataGridViewForm, id, name, soLuong.ToString(), gia); dataGridViewForm.Rows.Add(row); } // cộng vào tổng tiền hiện tại currentPrice += gia * soLuong; txtPrice.Text = currentPrice.ToString(); } } }
//private void dataGridViewCT_CellValueChanged(object sender, DataGridViewCellEventArgs e) //{ // int changedCol = e.ColumnIndex; // int remainCol = 2; // if (changedCol == 2) // so luong bi thay doi // remainCol = 3; // int a; // int.TryParse(dataGridViewCT.Rows[e.RowIndex].Cells[changedCol].Value.ToString(), out a); // int b; // int.TryParse(dataGridViewCT.Rows[e.RowIndex].Cells[remainCol].Value.ToString(), out b); // int result; // int.TryParse(txtPrice.Text, out result); // result += (a * b); // txtPrice.Text = result.ToString(); //} private void dataGridViewCT_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (mode) { int oldSoLuong = int.Parse(dataGridViewCT.Rows[e.RowIndex].Cells[1].Value.ToString()); int oldGiaGoc = int.Parse(dataGridViewCT.Rows[e.RowIndex].Cells[2].Value.ToString()); int oldTong = int.Parse(txtPrice.Text); using (AddProductToInvoiceConfirmForm confirmForm = new AddProductToInvoiceConfirmForm(oldSoLuong.ToString(), oldGiaGoc.ToString())) { if (confirmForm.ShowDialog() == DialogResult.OK) { oldTong = oldTong - (oldSoLuong * oldGiaGoc); oldTong += (confirmForm.SoLuong * confirmForm.GiaGoc); txtPrice.Text = oldTong.ToString(); dataGridViewCT.Rows[e.RowIndex].Cells[1].Value = confirmForm.SoLuong; dataGridViewCT.Rows[e.RowIndex].Cells[2].Value = confirmForm.GiaGoc; } } } }