private void BtnAdd_Click(object sender, EventArgs e) { //O day la phuing cap Disconnect cua Microsoft //Tuc la da se thay doi du lieu tren DataSource cua client //Sau do goi server de add mot gia tri moi ma khong can phai load lai Data //1. Khoi tao gia tri mac dinh cho Product int ID = 1; string Name = string.Empty; float Price = 0; int productQuantity = 0; if (dtProduct.Rows.Count > 0) { ID = int.Parse(dtProduct.Compute("MAX(ProductID)", "").ToString()) + 1; } Product pro = new Product { ProductID = ID, Quantity = productQuantity, UnitPrice = Price, ProductName = Name }; //2. Tao doi tuong moi va truyen du lieu frmProductDetails productDetails = new frmProductDetails(true, pro, productDb); //3. Lay ket qua tra ve DialogResult r = productDetails.ShowDialog(); if (r == DialogResult.OK) { pro = productDetails.ProductAddOrEdit; //Cap nhat vao DataTable dtProduct.Rows.Add(pro.ProductID, pro.ProductName, pro.UnitPrice, pro.Quantity); } }
private void BtnUpdate_Click(object sender, EventArgs e) { int ID = int.Parse(txtBookID.Text); string Title = txtBookTitle.Text; float Price = float.Parse(txtBookPrice.Text); int Quantity = int.Parse(txtBookQuantity.Text); Product pro = new Product { ProductID = ID, ProductName = Title, UnitPrice = Price, Quantity = Quantity }; //goi phuong thuc cap nhat frmProductDetails productDetails = new frmProductDetails(false, pro, productDb); //3. Lay ket qua tra ve DialogResult r = productDetails.ShowDialog(); if (r == DialogResult.OK) { DataRow row = dtProduct.Rows.Find(pro.ProductID); row["ProductName"] = pro.ProductName; row["Quantity"] = pro.Quantity; row["UnitPrice"] = pro.UnitPrice; } }