📄 customersinfoform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagerModels;
using HotelManagerBLL;
namespace HotelManager
{
public partial class CustomersInfoForm : Form
{
private int i = 0;//通过变量来判断要执行功能: 1.添加 2.修改
public static int IsFormOpen = 0;//判断窗体是否打开,0没打开,1,打开
CustomersInfoBLL bll = new CustomersInfoBLL();
public CustomersInfoForm()
{
IsFormOpen = 1;
InitializeComponent();
}
private void CustomersInfoForm_Load(object sender, EventArgs e)
{
panInfo.Visible = false;
try
{
//查询所有客户信息
List<CustomersInfo> list = bll.SelectCustomersInfoAll();
dgvCustomersInfo.DataSource = list;
//绑定combox数据源,客户类型
CustomersTypeBLL ctBLL = new CustomersTypeBLL();
List<CustomersType> ctlist = ctBLL.SelectCustomersType();
cboCTypeID.DataSource = ctlist;
cboCTypeID.DisplayMember = "Name";
cboCTypeID.ValueMember = "ID";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dgvCustomersInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
panInfo.Visible = true;
panInfo.Enabled = false;
//在文本框中,显示详细信息
txtID.Text = dgvCustomersInfo.CurrentRow.Cells["ID"].Value.ToString();
txtCName.Text = dgvCustomersInfo.CurrentRow.Cells["CName"].Value.ToString();
txtCPhone.Text = dgvCustomersInfo.CurrentRow.Cells["CPhone"].Value.ToString();
cboCTypeID.Text = "";
cboCTypeID.Text = dgvCustomersInfo.CurrentRow.Cells["CT"].Value.ToString();
}
private void tsBtnSave_Click(object sender, EventArgs e)
{
//判断文本框是否为空
if (txtID.Text.Trim() == "" || txtCName.Text.Trim() == "" || txtCPhone.Text.Trim() == "")
{
MessageBox.Show("请填写完整信息");
return;
}
else
{
//判断身份证号是否是15或18位
int index = txtID.Text.Trim().Length;
MessageBox.Show(index.ToString());
if (index== 18 || index==15)
{
//1,为添加信息
if (i == 1)
{
//判断此用户是否存在
List<CustomersInfo> infolist = bll.SelectCustomersInfoByID(txtID.Text.ToString());
if (infolist.Count > 0)
{
MessageBox.Show("该用户已存在!");
dgvCustomersInfo.DataSource = infolist;
}
else
{
//不存在,封装信息到实体类中
CustomersInfo ci = new CustomersInfo();
ci.ID = txtID.Text.ToString();
ci.CName = txtCName.Text.ToString();
ci.CPhone = txtCPhone.Text.ToString();
CustomersType ct = new CustomersType();
ct.ID = Convert.ToInt32(cboCTypeID.SelectedValue);
ci.Ct = ct;
try
{
//添加到数据库
bll.InsertCustomersInfo(ci);
//刷新DataGrildView
List<CustomersInfo> list = bll.SelectCustomersInfoAll();
dgvCustomersInfo.DataSource = list;
panInfo.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//2为修改
if (i == 2)
{
//封装信息到实体类中
CustomersInfo ci = new CustomersInfo();
ci.ID = txtID.Text.ToString().Trim();
ci.CName = txtCName.Text.ToString();
ci.CPhone = txtCPhone.Text.ToString();
CustomersType ct = new CustomersType();
ct.ID = Convert.ToInt32(cboCTypeID.SelectedValue);
ci.Ct = ct;
try
{
//根据身份证号修改客户信息
bool b = bll.UpdateCustomersInfo(ci);
if (b)
{
//修改成功,刷新DataGrildView
List<CustomersInfo> list = bll.SelectCustomersInfoAll();
dgvCustomersInfo.DataSource = list;
panInfo.Enabled = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
{
MessageBox.Show("输入的身份证号码有误");
return;
}
}
}
private void tsBntAdd_Click(object sender, EventArgs e)
{
//改变窗体状态为添加状态
this.ClientSize = new System.Drawing.Size(505, 427);
panInfo.Visible = true;
panInfo.Enabled = true;
txtID.Enabled = true;
i = 1;//确定为添加
txtID.Text = "";
txtCName.Text = "";
txtCPhone.Text = "";
cboCTypeID.Text = "普通用户";
}
private void tsBntUpdate_Click(object sender, EventArgs e)
{
//改变窗体状态为修改状态
this.ClientSize = new System.Drawing.Size(505, 427);
panInfo.Visible = true;
panInfo.Enabled = true;
txtID.Enabled = false;
i = 2;//确定为修改
//提供要修改的信息
txtID.Text = dgvCustomersInfo.CurrentRow.Cells["ID"].Value.ToString();
txtCName.Text = dgvCustomersInfo.CurrentRow.Cells["CName"].Value.ToString();
txtCPhone.Text = dgvCustomersInfo.CurrentRow.Cells["CPhone"].Value.ToString();
cboCTypeID.Text = "";
cboCTypeID.Text = dgvCustomersInfo.CurrentRow.Cells["CT"].Value.ToString();
}
private void tsBtnDelete_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("确定要删除吗?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
string id = dgvCustomersInfo.CurrentRow.Cells["ID"].Value.ToString();
try
{
bll.DeleteCustomersInfo(id);
//刷新
List<CustomersInfo> list = bll.SelectCustomersInfoAll();
dgvCustomersInfo.DataSource = list;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void txtID_Leave(object sender, EventArgs e)
{
try
{
//判断用户是否存在
List<CustomersInfo> list = bll.SelectCustomersInfoByID(txtID.Text.ToString());
if (list.Count > 0)
{
MessageBox.Show("改用户已存在");
dgvCustomersInfo.DataSource = list;
txtID.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
//模糊查询
List<CustomersInfo> list = bll.SelectCustomersInfoByCID(txtSearch.Text.ToString().Trim());
dgvCustomersInfo.DataSource = list;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tsBtnCancel_Click(object sender, EventArgs e)
{
//取消
CustomersInfoForm_Load(sender,e);
}
private void tsBtnExit_Click(object sender, EventArgs e)
{
IsFormOpen = 0;
this.Close();
}
private void CustomersInfoForm_FormClosing(object sender, FormClosingEventArgs e)
{
IsFormOpen = 0;
}
private void txtID_KeyPress(object sender, KeyPressEventArgs e)
{
//用户输入身份证号码是只能输入数字
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
{
e.Handled = true;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -