/// <summary> /// 编辑用户 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnEditClick(object sender, EventArgs e) { //1收集网格上旧的数据源 var curUser = _userList.Single(c => c.Id == listBoxUser.SelectedValue.ToString()); //2手机用户修改后的信息 var formUserInfo = new FormRegister(curUser.Id, curUser.Name, curUser.Password); var dialogResult = formUserInfo.ShowDialog(); if (dialogResult != DialogResult.OK) return; //3校验信息 var user = _userList.Single(c => c.Id == curUser.Id); if (curUser.Id != formUserInfo.UserInfo.Id && _userList.Any(c=>c.Id==formUserInfo.UserInfo.Id)) { MessageBox.Show(@"用户编号已存在,请重新修改。"); return; } //4保存信息 user.Id = formUserInfo.UserInfo.Id; user.Name = formUserInfo.UserInfo.Name; user.Password = formUserInfo.UserInfo.Password; bindingSource1.ResetCurrentItem(); //通过数据源组件重设当前项 }
/// <summary> /// 新增用户 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnAddClick(object sender, EventArgs e) { //1获取User对象 var formUserInfo = new FormRegister(); var dialogResult = formUserInfo.ShowDialog(); if (dialogResult != DialogResult.OK) return; var userInfo = formUserInfo.UserInfo; //2验证数据 if (_userList.Any(user => user.Id == userInfo.Id)) { MessageBox.Show(@"新增失败,用户编号已经存在!"); return; } if (_userList.Any(user => user.Name == userInfo.Name)) { MessageBox.Show(@"新增失败,用户姓名已经存在!"); return; } //3通过数据绑定组件同步界面和数据源 bindingSource1.Add(userInfo); }