/// <summary> /// Sets the cell san pham With SanPhamModel from outsite /// We need to save it to model of ChiTietHoaDon /// One HoaDon complete - create and pham /// </summary> /// <param name="value">The value.</param> public void SetCellSanPham(SanPhamModel value) { if (dataGridView != null) { if (dataGridView.Rows.Count > 0) { if (currentRowSelected != -1) { if (currentRowSelected < dataGridView.Rows.Count) { dataGridView.Rows[currentRowSelected].Selected = true; } else { currentRowSelected = 0; } } dataGridView.Rows[currentRowSelected].Cells[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].Value = (value == null) ? "-1" : value.MaGiayDep; if (value != null) { var m = dataGridView.Rows[currentRowSelected].Cells; for (int i = 0; i < currentListDataTable.Count; i++) { if (currentListDataTable.ElementAt(i).IDModel == m[Rf.Name <ChiTietHdnModel>(c => c.IDModel)].Value.ToString()) { currentListDataTable.ElementAt(i).SanPham = value.ToEntity(); } } } } } }
/// <summary> /// Handles the Click event of the bntXoa control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> private void bntXoa_Click(object sender, EventArgs e) { List <DataGridViewRow> selectedRows = (from row in dataGridView.Rows.Cast <DataGridViewRow>() where Convert.ToBoolean(row.Cells["CB"].Value) select row).ToList(); if ( MessageBox.Show(string.Format("Bạn có chắc chắn xóa {0} đơn hàng này không?", selectedRows.Count), Resources.View_Confirm, MessageBoxButtons.YesNo) == DialogResult.Yes) { var s = (from row in dataGridView.Rows.Cast <DataGridViewRow>() where Convert.ToBoolean(row.Cells["CB"].Value) select row.Index).ToList(); if (s.Count == 0) { return; } for (int k = s.Count - 1; k >= 0; k--) { var i = s.ElementAt(k); var mx = dataGridView.Rows[i].Cells; for (int j = 0; j < currentListDataTable.Count; j++) { var m = currentListDataTable.ElementAt(j); if (m.IDModel == mx[Rf.Name <ChiTietHdbModel>(c => c.IDModel)].Value) { currentListDataTable.RemoveAt(j); break; } } dataSourceDataTable.Rows.RemoveAt(i); } //var dt = (DataTable) dataBindingSource.DataSource; //dataGridView.Rows.RemoveAt(); //dataGridView.Rows.Clear(); //controller.Delete(string.Join(",", s.ToArray())); //controller.ReviewGrid(); } if (dataSourceDataTable.Rows.Count == 0) { bntXoa.Enabled = false; bntLuaChon.Enabled = false; //bntLuu.Enabled = false; //bntTaoMoi.Enabled = true; } }
/// <summary> /// Adds the new template. /// </summary> /// <param name="table">The table.</param> public void AddNewTemplate(DataTable table) { var m = table.NewRow(); var a = TemplateNew(); //todo: check now currentListDataTable.Add(a); m[Rf.Name <ChiTietHdnModel>(c => c.ID)] = a.ID; m[Rf.Name <ChiTietHdnModel>(c => c.DonGia)] = a.DonGia; m[Rf.Name <ChiTietHdnModel>(c => c.GiamGia)] = a.GiamGia; m[Rf.Name <ChiTietHdnModel>(c => c.SoLuong)] = a.SoLuong; m[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)] = a.MaGiayDep; m[Rf.Name <ChiTietHdnModel>(c => c.ThanhTien)] = a.ThanhTien; m[Rf.Name <ChiTietHdnModel>(c => c.IDModel)] = a.IDModel; table.Rows.Add(m); //table.AcceptChanges(); }
/// <summary> /// Handles the Click event of the bntTaoMoi control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void bntTaoMoi_Click(object sender, EventArgs e) { bntLuu.Enabled = true; bntTaoMoi.Enabled = true; bntXoa.Enabled = true; bntLuaChon.Enabled = true; currentListDataTable = currentListDataTable ?? new List <ChiTietHdnModel> { TemplateNew() }; if (dataGridView.Rows.Count == 0) { dataSourceDataTable = currentListDataTable.ToDataTable(); dataSourceDataTable.AcceptChanges(); dataBindingSource.DataSource = dataSourceDataTable; dataGridView.DataSource = dataBindingSource; dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.ID)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.SanPhamID)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.SanPham)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.HoaDonNhapID)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.HoaDonNhap)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.SoHDN)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.IDModel)].Display(false); if (dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)] != null) { dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].DisplayIndex = 1; } } else { AddNewTemplate((DataTable)dataBindingSource.DataSource); } }
/// <summary> /// DGV_s the cell click. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="DataGridViewCellEventArgs" /> instance containing the event data.</param> private void dataGridView1_ChangedValue(Object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } int row = e.RowIndex; int col = e.ColumnIndex; currentRowSelected = row; // from 1 no 0 var m = dataGridView.Rows[currentRowSelected].Cells; if (col > 0) { if (dataGridView.Columns[col].Name == Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)) { var id = m[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].Value.ToString(); if (id != "-1" && id != "") { var RowIDs = (from rowx in dataGridView.Rows.Cast <DataGridViewRow>() where (rowx.Cells[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].Value.ToString() == id) select rowx).ToList(); if (RowIDs.Count() > 1) { MessageBox.Show("Mã sản phẩm đã tồn tại!"); m[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].Value = "-1"; return; } var noex = controller.CheckMaFromSanPham(id); if (noex == null) { if (MyDialogs.Question("Mã sản phẩm chưa tồn tại, bạn muốn khai báo không?") == false) { m[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].Value = "-1"; } else { controller.ShowSanPhamViewToCreate(id); } } else { // get back đơn giá nhập! dataGridView.Rows[currentRowSelected].Cells[Rf.Name <ChiTietHdnModel>(c => c.DonGia)].Value = noex.DonGiaNhap; } } } var a = m[Rf.Name <ChiTietHdnModel>(c => c.SoLuong)].Value.ToInt32(); var b = m[Rf.Name <ChiTietHdnModel>(c => c.DonGia)].Value.ToDecimal(); var d = m[Rf.Name <ChiTietHdnModel>(c => c.GiamGia)].Value.ToDecimal(); if (d > b) { m[Rf.Name <ChiTietHdnModel>(c => c.GiamGia)].Value = d; } var total = a * (b - d); m[Rf.Name <ChiTietHdnModel>(c => c.ThanhTien)].Value = total; } var sum = (from r in dataGridView.Rows.Cast <DataGridViewRow>() select r.Cells[Rf.Name <ChiTietHdnModel>(c => c.ThanhTien)].Value.ToDecimal()).Sum(c => c); txtTongTien.Text = sum.ToString(CultureInfo.InvariantCulture); if (currentListDataTable != null) { for (int i = 0; i < currentListDataTable.Count; i++) { if (currentListDataTable.ElementAt(i).IDModel == m[Rf.Name <ChiTietHdnModel>(c => c.IDModel)].Value.ToString()) { currentListDataTable.ElementAt(i).DonGia = m[Rf.Name <ChiTietHdnModel>(c => c.DonGia)].Value.ToDecimal(); currentListDataTable.ElementAt(i).GiamGia = m[Rf.Name <ChiTietHdnModel>(c => c.GiamGia)].Value.ToDecimal(); currentListDataTable.ElementAt(i).SoLuong = m[Rf.Name <ChiTietHdnModel>(c => c.SoLuong)].Value.ToInt32(); currentListDataTable.ElementAt(i).MaGiayDep = m[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].Value.ToString(); currentListDataTable.ElementAt(i).ThanhTien = m[Rf.Name <ChiTietHdnModel>(c => c.ThanhTien)].Value.ToDecimal(); } } currentHoaDonNhapModel.TongTien = txtTongTien.Text.ToDecimal(); currentHoaDonNhapModel.ChiTietHDNModel = currentListDataTable; } }
/// <summary> /// Posts the view. /// </summary> /// <param name="value">The value.</param> public void PostView(HoaDonNhapModel value) { currentHoaDonNhapModel = value; currentListDataTable = null; dataGridView.Columns.Clear(); var g = new DataGridViewCheckBoxColumn { Name = "CB", HeaderText = "", Width = 24, AutoSizeMode = DataGridViewAutoSizeColumnMode.None, ReadOnly = true }; dataGridView.Columns.Add(g); dataSourceDataTable = value.ChiTietHDNModel.ToDataTable(); dataSourceDataTable.AcceptChanges(); dataBindingSource.DataSource = dataSourceDataTable; dataGridView.DataSource = dataBindingSource; //dataGridView.DataSource = new BindingSource { DataSource = value.ChiTietHDNModel }; dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.ID)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.SanPhamID)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.SanPham)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.HoaDonNhapID)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.HoaDonNhap)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.SoHDN)].Display(false); dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.IDModel)].Display(false); if (dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)] != null) { dataGridView.Columns[Rf.Name <ChiTietHdnModel>(c => c.MaGiayDep)].DisplayIndex = 1; } if (value != null) { if (value.SoHDN != null) { txtMaHoaDon.Text = value.SoHDN; } if (value.MaNV != null) { txtMaNhanVien.Text = value.MaNV; } if (value.MaNCC != null) { txtMaNCC.Text = value.MaNCC; } if (value.NgayNhap != null) { txtNgayNhap.Text = value.NgayNhap.ToDateString(); } if (value.TongTien != null) { txtTongTien.Text = value.TongTien.ToString(); } } txtMaNhanVien.Text = AppMediator.MANHANVIEN; dataGridView.ClearSelection(); dataGridView.CurrentCell = null; if (isEditMode) { bntLuu.Enabled = false; bntTaoMoi.Enabled = true; bntXoa.Enabled = false; bntLuaChon.Enabled = false; txtMaHoaDon.Focus(); } else { bntLuu.Enabled = false; bntTaoMoi.Enabled = false; bntXoa.Enabled = false; } currentModel = null; }