private void BindModel(UserModel user)
        {
            BindingSource bsUser = new BindingSource();
            bsUser.DataSource = user;

            Username.DataBindings.Add("EditValue", bsUser, user.GetName(p => p.Username), true, DataSourceUpdateMode.OnPropertyChanged);
            Password.DataBindings.Add("EditValue", bsUser, user.GetName(p => p.Password), true, DataSourceUpdateMode.OnPropertyChanged);

            FirstName.DataBindings.Add("EditValue", bsUser, user.GetName(p => p.FirstName), true, DataSourceUpdateMode.OnPropertyChanged);
            SecondName.DataBindings.Add("EditValue", bsUser, user.GetName(p => p.SecondName), true, DataSourceUpdateMode.OnPropertyChanged);
            LastName.DataBindings.Add("EditValue", bsUser, user.GetName(p => p.LastName), true, DataSourceUpdateMode.OnPropertyChanged);

            IsEnabled.DataBindings.Add("Checked", bsUser, user.GetName(p => p.IsEnabled), true, DataSourceUpdateMode.OnPropertyChanged);
            IsAdmin.DataBindings.Add("Checked", bsUser, user.GetName(p => p.IsAdmin), true, DataSourceUpdateMode.OnPropertyChanged);
        }
        public static CheckResult ValidateSave(DSModel db, UserModel user)
        {
            CheckResult res = new CheckResult();
            if (string.IsNullOrWhiteSpace(user.Username))
                res.AddError("Please enter a Username!", user.GetName(p => p.Username));
            if (string.IsNullOrWhiteSpace(user.FirstName))
                res.AddError("Please enter a First Name!", user.GetName(p => p.FirstName));
            if (string.IsNullOrWhiteSpace(user.LastName))
                res.AddError("Please enter a Last Name!", user.GetName(p => p.LastName));

            var check = db.Users.Where(u => u.Username == user.Username && u.UserID != user.UserID).FirstOrDefault();
            if (check != null)
                res.AddError("Another user already uses this username!", user.GetName(p => p.Username));

            return res;
        }
        public CheckResult SaveUser(UserModel user)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = UserValidator.ValidateSave(db, user);
                    if (check.Failed)
                        return check;

                    KeyBinder key = new KeyBinder();
                    UserRepository.SaveUser(db, key, user);
                    db.SaveChanges();
                    key.BindKeys();
                    user.IsChanged = false;
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
 private UserManager(DSModel context, UserModel user)
     : base(context)
 {
     this.ActiveModel = user;
 }
        public static UserManager CreateEdit(UserModel user)
        {
            var db = DB.GetContext();

            return new UserManager(db, user);
        }