using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CaterBll;
using CaterModel;
namespace CaterUI
{
public partial class FormManageInfo : Form
{
// 对这个类实现单例(构造函数私有化)
private FormManageInfo()
{
InitializeComponent();
}
// 单例的实现
private static FormManageInfo _form;
// 静态方法获取单例
public static FormManageInfo CreateSigleInstance()
{
if (_form == null)
{
_form = new FormManageInfo();
}
return _form;
}
//创建业务逻辑层对象,得到查询到的结果
ManagerInfoBll miBll = new ManagerInfoBll();
private void FormManageInfo_Load(object sender, EventArgs e)
{
//准备数据
LoadList();
}
//加载数据
private void LoadList()
{
//默认的表格只有三列,但是从数据库中如果查询到多列,就会自动生成,可取消自动生成
dgvList.AutoGenerateColumns = false;
//设置查询结果刚好充满整个表格
dgvList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
//在UI层显示查询到的结果,绑定到列表的数据源
dgvList.DataSource = miBll.GetList();
}
private void btnAdd_Click(object sender, EventArgs e)
{
//不管添加还是修改,文本框里的内容都是要显示的
ManagerInfo mi = new ManagerInfo()
{
MName = this.txtName.Text.ToString(),
MPwd = this.txtPwd.Text.ToString(),
//经理为1,店员为0
MType = this.rbManager.Checked ? 1 : 0
};
if (btnAdd.Text == "添加" || txtId.Text.Equals("添加时无编号"))
{
#region 添加信息
//实现添加数据
if (this.txtName.Text == "" || this.txtPwd.Text == "")
{
MessageBox.Show("请将用户名或密码填写完整!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//开始调用BLL层的Add方法
if (miBll.Add(mi))
{
//添加成功之后,就重新加载表中的数据
LoadList();
}
else
{
MessageBox.Show("添加失败,请稍后重试!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
else
{
#region 修改信息
//根据用户的ID号码去修改信息
mi.MId = int.Parse(this.txtId.Text);
if (miBll.Edit(mi))
{
//添加成功后重新加载列表
LoadList();
//修改成功,给一个提示信息
MessageBox.Show("密码修改成功,请使用新密码!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//恢复到最初的状态
txtId.Text = "添加时无编号";
rbEmployer.Checked = true;
btnAdd.Text = "添加";
}
else
{
//添加失败
MessageBox.Show("修改失败,请稍后重试!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
//添加/修改完成之后,重置文本框
this.txtName.Text = "";
this.txtPwd.Text = "";
}
//只是在显示的时候改变了效果,表中数据并没有改变
private void dgvList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//查询出来结果后,格式化内容
if (e.ColumnIndex == 2)
{
//类型列
e.Value = Convert.ToInt32(e.Value) == 1 ? "经理" : "店员";
}
}
//单元格双击
private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//在某一个单元格双击的时候
//只有在双击内容列才会有效果,双击其他地方没反应
if (e.RowIndex >= 0)
{
//根据点击的单元格,找到行与列,进行赋值
DataGridViewRow row = dgvList.Rows;
//拿到这行后,就可以操作列了
this.txtId.Text = row.Cells.Value.ToString(); //id列
this.txtName.Text = row.Cells.Value.ToString(); //name列
if (row.Cells.Value.ToString().Equals("1"))
{
this.rbManager.Checked = true;
}
else
{
this.rbEmployer.Checked = true;
}
//开始指定密码的值
this.txtPwd.Text = "这是原来的密码吗?";
//按钮改变了
this.btnAdd.Text = "修改";
}
}
///
/// 注意在删除前记得把 list的MultiSelect属性设置为false(不允许选中多行,只能根据用户单行选中的行来删除数据)
///
///
///
private void btnDelete_Click(object sender, EventArgs e)
{
//删除操作
//获取选中的行
var rows = dgvList.SelectedRows;
if (rows.Count > 0)
{
//删除前的提示操作
DialogResult dr = MessageBox.Show("确认要删除吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Cancel)
{
//用户取消删除
return;
}
//获取选中行的编号
string s_id = rows.Cells.Value.ToString();
int id = int.Parse(s_id);
///开始删除
if (miBll.Remove(id))
{
//删除成功
LoadList();
}
}
else
{
MessageBox.Show("请先选择要删除的行", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
//取消操作
this.txtId.Text = "添加时编号不可修改";
this.txtName.Text = "";
this.txtPwd.Text = "";
this.rbEmployer.Checked = true;
this.btnAdd.Text = "添加";
}
// 当我的这个主窗体被关闭了以后, 就把_form 指向为null
private void FormManageInfo_FormClosing(object sender, FormClosingEventArgs e)
{
// 与单例保持一致
// 出现这种代码的原因: Form的close()方法会自动释放当前的窗体对象()
_form = null;
}
}
}
评论
填写昵称与邮箱即可评论,无需登录。