Example #1
0
        protected void AddDetail(int contractid, bool isAdd)
        {
            dataGridView1.EndEdit();

            List <FishEntity.ContractDetailEntity> listNews = new List <FishEntity.ContractDetailEntity>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow)
                {
                    continue;
                }
                FishEntity.ContractDetailEntity item = new FishEntity.ContractDetailEntity();
                int id = 0;
                int.TryParse(row.Cells["id"].Value == null ? "0" : row.Cells["id"].Value.ToString(), out id);
                item.id = id;
                int no = 0;
                int.TryParse(row.Cells["no"].Value == null ? "0" : row.Cells["no"].Value.ToString(), out no);
                item.no = no;
                int productid = 0;
                int.TryParse(row.Cells["productid"].Value == null ? "0" : row.Cells["productid"].Value.ToString(), out productid);
                item.productid     = productid;
                item.productno     = row.Cells["productno"].Value == null ? string.Empty : row.Cells["productno"].Value.ToString();
                item.productname   = row.Cells["productname"].Value == null ? string.Empty : row.Cells["productname"].Value.ToString();
                item.specification = row.Cells["specification"].Value == null ? string.Empty : row.Cells["specification"].Value.ToString();
                item.remark        = row.Cells["ctlremark"].Value == null ? string.Empty : row.Cells["ctlremark"].Value.ToString();
                item.nature        = row.Cells["nature"].Value == null ? string.Empty : row.Cells["nature"].Value.ToString();
                decimal weight = 0;
                if (row.Cells["weight"].Value == null)
                {
                    weight = 0;
                }
                else
                {
                    decimal.TryParse(row.Cells["weight"].Value.ToString(), out weight);
                }
                item.weight = weight;

                int quantity = 0;
                if (row.Cells["quantity"].Value == null)
                {
                    quantity = 0;
                }
                else
                {
                    int.TryParse(row.Cells["quantity"].Value.ToString(), out quantity);
                }
                item.quantity = quantity;

                decimal unitprice = 0;
                decimal.TryParse(row.Cells["unitprice"].Value == null ? "0.00" : row.Cells["unitprice"].Value.ToString(), out unitprice);
                item.unitprice = unitprice;
                decimal money = 0;
                decimal.TryParse(row.Cells["money"].Value == null ? "0.00" : row.Cells["money"].Value.ToString(), out money);
                item.money      = money;
                item.contractid = contractid;

                listNews.Add(item);
            }


            if (isAdd == false)
            {
                List <FishEntity.ContractDetailEntity> listsource = _detailbll.GetModelList(" contractid=" + contractid);
                if (listsource != null)
                {
                    foreach (FishEntity.ContractDetailEntity item in listsource)
                    {
                        bool isExist = listNews.Exists((i) => { return(i.id == item.id); });
                        if (isExist == false)
                        {
                            bool isDelete = _detailbll.Delete(item.id);
                        }
                    }
                }
            }

            foreach (FishEntity.ContractDetailEntity item in listNews)
            {
                if (item.id == 0)
                {
                    int id = _detailbll.Add(item);
                    if (id > 0)
                    {
                        item.id = id;
                    }
                }
                else
                {
                    _detailbll.Update(item);
                }
            }

            _details = listNews;
            SetDetail();
        }