private void InitDaSupplierContact()
        {
            var        rowIndex    = dgvDaSupplier.SelectedRows[0].Index;
            DaSupplier mDaSupplier = (DaSupplier)DaSupplierBindingSource[rowIndex];

            DaSuppContactBindingSource.DataSource = myEntity.DaSupplierContacts.Where(d => d.IntSupplierID == mDaSupplier.IntID).AsNoTracking().ToList();// mDaSupplier.DaSupplierContacts.ToList();
        }
        private void dgvDaSupplier_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            if (e.ColumnIndex == dsc删除.Index)
            {
                //删除
                int        daSupplierId = ((DaSupplier)DaSupplierBindingSource[e.RowIndex]).IntID;
                DaSupplier mDaSupplier  = myEntity.DaSuppliers.SingleOrDefault(a => a.IntID == daSupplierId);
                if (mDaSupplier.DaPurchaseContracts.Count > 0)
                {
                    MessageBox.Show("该物品已有采购订单,不能删除");
                    return;
                }
                if (MessageBox.Show("是否确认删除" + mDaSupplier.VcName, "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }
                //myEntity.DaSuppliers.Attach(mDaSupplier);
                mDaSupplier.DaGoodsSuppliers.Clear();
                myEntity.DaSuppliers.Remove(mDaSupplier);

                int ret = myEntity.SaveChanges();

                if (ret > 0)
                {
                    InitDaGoodsSupplier();
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            if (e.ColumnIndex == dsc修改.Index)
            {
                //修改
                using (供应商编辑 mForm = new 供应商编辑())
                {
                    mForm.DaSupplierID = ((DaSupplier)DaSupplierBindingSource[e.RowIndex]).IntID;

                    if (mForm.ShowDialog() == DialogResult.OK)
                    {
                        InitDaGoodsSupplier();
                    }
                }
            }
        }
        private void 增加供应商_Load(object sender, EventArgs e)
        {
            if (myEntity == null)
            {
                myEntity = new MYDBEntities2();
            }
            if (DaSupplierID == 0)
            {
                //新增
                string maxNum = myEntity.DaSuppliers.Select(a => a.VcNum).Max();

                maxNum = !string.IsNullOrEmpty(maxNum) ? (Convert.ToInt64(maxNum) + 1).ToString() : "1000001";

                txtDaSupplierNum.Text = maxNum;
            }
            else
            {
                //修改
                //根据DaSupplierID获取供应商
                mDaSupplier         = myEntity.DaSuppliers.Where(d => d.IntID == DaSupplierID).SingleOrDefault();
                txtName.Text        = mDaSupplier.VcName;
                txtRegCapital.Text  = mDaSupplier.VcRegCapital;
                txtLegalRep.Text    = mDaSupplier.VcLegalRep;
                txtTaxID.Text       = mDaSupplier.VcTaxID;
                txtOpeningBank.Text = mDaSupplier.VcOpeningBank;
                txtBankAccount.Text = mDaSupplier.VcBankAccount;
                txtAddress.Text     = mDaSupplier.VcAddress;
                txtWebsite.Text     = mDaSupplier.VcWebsite;
                txtTel.Text         = mDaSupplier.VcTel;
                txtFax.Text         = mDaSupplier.VcFax;
                txtPostCode.Text    = mDaSupplier.VcPostCode;
                txtRemark.Text      = mDaSupplier.VcRemark;

                //DaSupplierbindingSource.DataSource = mDaSupplier;
                //txtName.DataBindings.Add("Text", DaSupplierbindingSource, "VcName");
                //txtRegCapital.DataBindings.Add("Text", DaSupplierbindingSource, "VcRegCapital");
            }
        }
        private void InitData()
        {
            if (myEntity == null)
            {
                myEntity = new MYDBEntities2();
            }

            if (DaContactID > 0)
            {
                //修改
                mDaSuppContact = myEntity.DaSupplierContacts.Where(d => d.IntID == DaContactID).SingleOrDefault();
                DaSupplierID   = mDaSuppContact.IntSupplierID;
            }
            else
            {
                //增加
                mDaSuppContact = new DaSupplierContact()
                {
                    IntSupplierID = DaSupplierID, IntEmpNum = Classes.PubClass.UserId
                };
                DaSupplier daSupplier = myEntity.DaSuppliers.SingleOrDefault(d => d.IntID == DaSupplierID);
                if (daSupplier != null)
                {
                    mDaSuppContact.VcTel = daSupplier.VcTel;
                    mDaSuppContact.VcFax = daSupplier.VcFax;
                }
            }
            bindingSourceDaSuppContact.DataSource = mDaSuppContact;
            txtName.Text = myEntity.DaSuppliers.Where(d => d.IntID == DaSupplierID).SingleOrDefault().VcName;
            txtContactName.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcName");
            txtPost.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcPost");
            txtTel.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcTel");
            txtFax.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcFax");
            txtMobile.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcMobile");
            txtQQ.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcQQ");
            txtEmail.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcEmail");
            txtRemark.DataBindings.Add("Text", bindingSourceDaSuppContact, "VcRemark");
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (DaSupplierID == 0)
                {
                    mDaSupplier = new DaSupplier()
                    {
                        VcName        = txtName.Text.Trim(),
                        VcRegCapital  = txtRegCapital.Text.Trim(),
                        VcLegalRep    = txtLegalRep.Text.Trim(),
                        VcTaxID       = txtTaxID.Text.Trim(),
                        VcOpeningBank = txtOpeningBank.Text.Trim(),
                        VcBankAccount = txtBankAccount.Text.Trim(),
                        VcAddress     = txtAddress.Text.Trim(),
                        VcWebsite     = txtWebsite.Text.Trim(),
                        VcTel         = txtTel.Text.Trim(),
                        VcFax         = txtFax.Text.Trim(),
                        VcPostCode    = txtPostCode.Text.Trim(),
                        VcRemark      = txtRemark.Text.Trim(),
                        IntEmpNum     = Classes.PubClass.UserId,
                        VcNum         = txtDaSupplierNum.Text.Trim(),
                        IntStatus     = 1
                    };

                    string maxNum = myEntity.DaSuppliers.Select(a => a.VcNum).Max();
                    //获取最新供应商编号
                    maxNum            = !string.IsNullOrEmpty(maxNum) ? (Convert.ToInt64(maxNum) + 1).ToString() : "1000001";
                    mDaSupplier.VcNum = maxNum;
                    //mDaSupplier.VcName = txtName.Text.Trim();
                    //mDaSupplier.VcRegCapital = txtRegCapital.Text.Trim();
                    //mDaSupplier.VcLegalRep = txtLegalRep.Text.Trim();
                    //mDaSupplier.VcTaxID = txtTaxID.Text.Trim();
                    //mDaSupplier.VcOpeningBank = txtOpeningBank.Text.Trim();
                    //mDaSupplier.VcBankAccount = txtBankAccount.Text.Trim();
                    //mDaSupplier.VcAddress = txtAddress.Text.Trim();
                    //mDaSupplier.VcWebsite = txtWebsite.Text.Trim();
                    //mDaSupplier.VcTel = txtTel.Text.Trim();
                    //mDaSupplier.VcFax = txtFax.Text.Trim();
                    //mDaSupplier.VcPostCode = txtPostCode.Text.Trim();
                    //mDaSupplier.VcRemark = txtRemark.Text.Trim();
                    //mDaSupplier.IntEmpNum = Classes.PubClass.UserId;
                    //mDaSupplier.VcNum = "1000001";
                    //DbEntityEntry<DaSupplier> entry = myEntity.Entry<DaSupplier>(mDaSupplier);
                    //entry.State = EntityState.Added;
                    myEntity.DaSuppliers.Add(mDaSupplier);
                    //myEntity.SaveChanges();
                }
                else
                {
                    mDaSupplier.VcName        = txtName.Text.Trim();
                    mDaSupplier.VcRegCapital  = txtRegCapital.Text.Trim();
                    mDaSupplier.VcLegalRep    = txtLegalRep.Text.Trim();
                    mDaSupplier.VcTaxID       = txtTaxID.Text.Trim();
                    mDaSupplier.VcOpeningBank = txtOpeningBank.Text.Trim();
                    mDaSupplier.VcBankAccount = txtBankAccount.Text.Trim();
                    mDaSupplier.VcAddress     = txtAddress.Text.Trim();
                    mDaSupplier.VcWebsite     = txtWebsite.Text.Trim();
                    mDaSupplier.VcTel         = txtTel.Text.Trim();
                    mDaSupplier.VcFax         = txtFax.Text.Trim();
                    mDaSupplier.VcPostCode    = txtPostCode.Text.Trim();
                    mDaSupplier.VcRemark      = txtRemark.Text.Trim();

                    DbEntityEntry <DaSupplier> entry = myEntity.Entry <DaSupplier>(mDaSupplier);
                    entry.State = EntityState.Unchanged;
                    entry.Property(a => a.VcName).IsModified        = true;
                    entry.Property(a => a.VcRegCapital).IsModified  = true;
                    entry.Property(a => a.VcLegalRep).IsModified    = true;
                    entry.Property(a => a.VcTaxID).IsModified       = true;
                    entry.Property(a => a.VcOpeningBank).IsModified = true;
                    entry.Property(a => a.VcBankAccount).IsModified = true;
                    entry.Property(a => a.VcAddress).IsModified     = true;
                    entry.Property(a => a.VcWebsite).IsModified     = true;
                    entry.Property(a => a.VcTel).IsModified         = true;
                    entry.Property(a => a.VcFax).IsModified         = true;
                    entry.Property(a => a.VcPostCode).IsModified    = true;
                    entry.Property(a => a.VcRemark).IsModified      = true;


                    //myEntity.SaveChanges();
                }
                //if (myEntity.Database.Connection.State == ConnectionState.Closed)
                //   myEntity.Database.Connection.Open();
                //using (var mTrans = myEntity.Database.Connection.BeginTransaction())
                using (TransactionScope mTrans = new TransactionScope())
                {
                    int ret = myEntity.SaveChanges();
                    if (ret > 0)
                    {
                        //MessageBox.Show("保存成功");
                        //return;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        return;
                    }
                    if (DaGoodsID > 0)
                    {
                        mDaGoodsSupplier = new DaGoodsSupplier()
                        {
                            IntGoodsID    = DaGoodsID,
                            IntSuppilerID = mDaSupplier.IntID
                        };
                        myEntity.DaGoodsSuppliers.Add(mDaGoodsSupplier);

                        ret = myEntity.SaveChanges();
                    }
                    if (ret > 0)
                    {
                        mTrans.Complete();
                        MessageBox.Show("保存成功");
                        DialogResult    = System.Windows.Forms.DialogResult.OK;
                        btnSave.Enabled = false;
                        return;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        return;
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                foreach (var item in ex.EntityValidationErrors)
                {
                    foreach (var item2 in item.ValidationErrors)
                    {
                        //MessageBox.Show(string.Format("{0}:{1}\r\n", item2.PropertyName, item2.ErrorMessage));
                        MessageBox.Show(string.Format("{0}\r\n", item2.ErrorMessage));
                    }
                    item.ValidationErrors.Clear();
                }
            }
        }