Exemplo n.º 1
0
        private void dgvList_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int id;

            if (int.TryParse(dgvList.Rows[e.RowIndex].Cells["ID"].Value.ToString(), out id))
            {
                if (isEFRun)
                {
                    EFClassLib.TableModel.UserInfor usInfor = null;
                    using (DBCTxt db = GetEFContext())
                    {
                        usInfor = db.UserInfor.Where(x => x.id == id).FirstOrDefault();

                        if (usInfor != null)
                        {
                            EFAddOrModifyUser(usInfor);
                        }
                        else
                        {
                            txtInfors.Text = "The User is not exists.";
                        }
                    }
                }
                else
                {
                    LDB.UserInfor usInfor = null;
                    using (var ldb = GetLinqContext())
                    {
                        usInfor = ldb.UserInfors.Where(x => x.id == id).FirstOrDefault();
                        if (usInfor != null)
                        {
                            LinqAddOrModifyUser(usInfor);
                        }
                        else
                        {
                            txtInfors.Text = "The User is not exists.";
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void EFAddOrModifyUser(EFClassLib.TableModel.UserInfor UserInf)
        {
            isEFRun = true;
            AddUser addusFrm = new AddUser();

            addusFrm.SetEFModiInfor(UserInf);

            if (UserInf == null)
            {
                if (addusFrm.ShowDialog() == DialogResult.OK)
                {
                    var usInfor = addusFrm.GetEFUserInfor();
                    usInfor.Address = new List <EFClassLib.TableModel.Address>();
                    usInfor.Address.Add(new EFClassLib.TableModel.Address {
                        addr = "yyyy", Email = "*****@*****.**", phone = "000000"
                    });
                    usInfor.Address.Add(new EFClassLib.TableModel.Address {
                        addr = "yyyy", Email = "*****@*****.**", phone = "000000"
                    });
                    usInfor.Address.Add(new EFClassLib.TableModel.Address {
                        addr = "yyyy", Email = "*****@*****.**", phone = "000000"
                    });
                    if (usInfor != null)
                    {
                        using (DBCTxt db = GetEFContext())
                        {
                            db.UserInfor.Add(usInfor);
                            db.SaveChanges();
                        }
                        RefreshDataByEF();
                    }
                    else
                    {
                        AddAndShowMessages("Nothing is Add");
                    }
                }
            }
            else
            {
                //addusFrm.SetEFModiInfor(UserInf);
                switch (addusFrm.ShowDialog())
                {
                case DialogResult.OK:
                    var usInfor = addusFrm.GetEFUserInfor();
                    if (usInfor != null)
                    {
                        using (DBCTxt db = GetEFContext())
                        {
                            var uif = db.UserInfor.Where(x => x.id == UserInf.id).FirstOrDefault();
                            if (uif != null)
                            {
                                uif.FirstName = usInfor.FirstName;
                                uif.LastName  = usInfor.LastName;
                                uif.Password  = usInfor.Password;
                                uif.UserName  = usInfor.UserName;
                                foreach (var addr in uif.Address)
                                {
                                    addr.Email = "*****@*****.**";
                                }
                                db.SaveChanges();
                            }
                        }
                        RefreshDataByEF();
                    }
                    else
                    {
                        AddAndShowMessages("Nothing is Modify");
                    }
                    break;

                case DialogResult.No:
                    using (DBCTxt db = GetEFContext())
                    {
                        db.Database.ExecuteSqlCommand("delete from UserInfors where id=@id"
                                                      , new SqlParameter("@id", UserInf.id)
                                                      );
                    }
                    RefreshDataByEF();
                    break;

                default:
                    break;
                }
            }
        }