三,客户模块设计
对客户管理模块的设计,要重点从三块来设计,一是添加模块,二是更改模块,三是浏览模块。
1,客户管理模块
客户管理模块在窗体上包含一个ToolStrip控件和一个DataGridView控件。ToolStrip控件中添加三个Label项,用来对DataGridView控件中所选择的行进行添加,删除和修改。
客户管理模块的设计示例为:
客户管理模块设计的窗体和控件的属性设置文档为:
接着就是要进行代码的写入:
(1)对于toolStrip1控件的代码写入是主要的,这里面包含添加,修改,删除三项操作:
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Name == "tslAdd")//添加
{
showForm(0);//弹出添加窗体
}
if (e.ClickedItem.Name == "tslMod")//修改
{
modChildren1();//弹出修改窗体
}
if (e.ClickedItem.Name == "tslDel")//删除
{
DialogResult status = MessageBox.Show("你确认删除所选的行?", "确定对话框", MessageBoxButtons.YesNo);
if (status == DialogResult.Yes)
{
delChildren1();//删除所选择的客户
}
}
}
(2)其次就是对添加窗体,修改窗体和删除三项内容的代码编辑,实际上客户信息添加和客户信息修改两个窗体本身是一个窗体。
完整的Children1窗体的程序代码为:
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 System.Data.SqlClient;//引入命名空间
namespace 客户管理系统
{
public partial class Children1 : Form
{
common cm = new common();//加载公共类common
database db=new database();//加载公共类database
public Children1()//构造函数
{
InitializeComponent();
}
private void Children1_Load(object sender, EventArgs e)//客户管理窗体的load事件
{
showCustomer();//显示客户
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Name == "tslAdd")//添加
{
showForm(0);//弹出添加窗体
}
if (e.ClickedItem.Name == "tslMod")//修改
{
modChildren1();//弹出修改窗体
}
if (e.ClickedItem.Name == "tslDel")//删除
{
DialogResult status = MessageBox.Show("你确认删除所选的行?", "确定对话框", MessageBoxButtons.YesNo);
if (status == DialogResult.Yes)
{
delChildren1();//删除所选择的客户
}
}
}
private void showForm(int t)//弹出添加或修改窗体
{
addChildren1 c1=new addChildren1();//创建添加客户或修改客户窗体的对象
c1.ID=t;
DialogResult result=c1.ShowDialog();
if (result==DialogResult.OK)
{
showCustomer();
}
}
private void modChildren1()//弹出修改客户资料窗体
{
if (dgv.RowCount>0)//存在行
{
if (dgv.SelectedRows.Count==1)//选择行
{
string value=dgv.SelectedRows[0].Cells[0].Value.ToString();
if (value!=null&&value!=string.Empty)//判定非空
{
showForm(int.Parse(value));//弹出修改窗体
}
else
{
MessageBox.Show("所选行不存在数据!");
}
}
else
{
MessageBox.Show("请选择一行要修改的数据!");
}
}
else
{
MessageBox.Show("目前还没有数据!");
}
}
private void delChildren1()//删除所选择客户
{
if (dgv.SelectedRows.Count>0)
{
for (int i = 0; i < dgv.SelectedRows.Count; i++)
{
string selectID = dgv.SelectedRows[1].Cells[0].Value.ToString();
if (selectID!=null&&selectID!=string.Empty)
{
db.runSql("delete customer where id="+selectID);
}
}
showCustomer();
}
}
private void showCustomer()//显示客户资料
{
cm.bindDgv(dgv, "select * from customer order by id desc");//以表格的形式显示客户资料
}
}
}
运行整个程序的结果显示为:
(1)点击菜单栏上的添加,显示添加客户信息的窗体:
(2)点击菜单栏上的修改,显示修改客户信息的窗体:
(3)点击菜单栏上的删除,显示删除客户信息的对话框: