/// <summary> /// 绑定所有列 /// </summary> protected override void BindDgdViewAll() { this.dgdViewAll.Rows.Clear(); //清除表格控件中的所有行 S_UserBll s_UserBll = new S_UserBll(); this._s_UserEntitys = s_UserBll.getUsers(); //选择当前表类型的所有列数据 foreach (S_UserEntity user in _s_UserEntitys) { string roleCn = ""; if (user.role == "role1") { roleCn = "管理员"; } else { roleCn = "操作员"; } this.dgdViewAll.Rows.Add(user.loginName, "******", user.realName, user.sex, user.email, user.ID, roleCn, user.ID); } if (_currentRow < 0 || _currentRow > this.dgdViewAll.Rows.Count) { _currentRow = 0; } dgdViewAll.CurrentCell = this.dgdViewAll.Rows[_currentRow].Cells[0]; }
/// <summary> /// 行的数据转为实体 /// </summary> /// <param name="row">行</param> /// <returns>OilTableColEntity实体</returns> private S_UserEntity rowToEntity(DataGridViewRow row) { S_UserEntity user = new S_UserEntity(); user.ID = int.Parse(row.Cells["ID"].Value.ToString()); if (user.ID != 0) { S_UserBll s_UserBll = new S_UserBll(); S_UserEntity userOld = s_UserBll.getUser(user.ID); user.password = RIPP.Lib.Security.SecurityTool.BuildPassword(row.Cells["password"].Value.ToString().Trim() == "******" ? userOld.password : row.Cells["password"].Value.ToString().Trim()); } else { user.password = RIPP.Lib.Security.SecurityTool.BuildPassword(row.Cells["password"].Value.ToString().Trim()); } user.loginName = row.Cells["loginName"].Value.ToString(); user.realName = row.Cells["realName"].Value == null ? "": row.Cells["realName"].Value.ToString(); user.sex = row.Cells["sex"].Value == null ? false : (bool)row.Cells["sex"].Value; user.tel = row.Cells["tel"].Value == null ? "" : row.Cells["tel"].Value.ToString(); user.email = row.Cells["email"].Value == null ? "" : row.Cells["email"].Value.ToString(); user.addTime = DateTime.Now; user.role = row.Cells["role"].Value == null ? "role1" : (row.Cells["role"].Value.ToString().Trim() == "管理员" ? "role1" : "role2"); return(user); }
/// <summary> /// 删除一行数据,重新绑定 /// </summary> protected override void toolStripBtnDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("是否要删除!", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) { int id = int.Parse(dgdViewAll.CurrentRow.Cells["ID"].Value.ToString()); if (id != 0) //如果要删除的行在数据库中存在(即ID字段不为0)则从数据库删除,否则(该行是才添加的还没存到数据库)从表格控件中删除行 { S_UserBll s_UserBll = new S_UserBll(); s_UserBll.deleteUser(id); _currentRow = 0; BindDgdViewAll(); } else { dgdViewAll.Rows.Remove(dgdViewAll.CurrentRow); } _isValidate = true; } }
/// <summary> /// 更新数据库,如果是新添加的行则添加数据库,否则更新数据库 /// </summary> /// <param name="col">OilTableColEntity实体</param> private void updateRow(S_UserEntity user) { S_UserBll s_UserBll = new S_UserBll(); if (user.ID != 0) //如果行在数据库中存在(即ID字段不为0)则从更新数据库,否则(该行是才添加的还没存到数据库)添加到数据库 { if (s_UserBll.updateUser(user) == -1) { MessageBox.Show("用户名重复!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { if (s_UserBll.addUser(user) == -1) { MessageBox.Show("用户名重复!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } }