笔记:
详情已经在前面的章节有列述了,这里只点出几个比较重要的:
string name = DataGridView对象.SelectedRows[0].Cells[0].Value.ToString();获取在DataGridView控件中选中的一行的第cells[0]列数据。
窗体样式:
代码如下:
主窗体,“手机销售管理系统”代码:
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;
namespace product6._10
{
public partial class FrmSalesManage : Form
{
public FrmSalesManage()
{
InitializeComponent();
}
private void 客户添加ToolStripMenuItem_Click(object sender, EventArgs e)
{
//客户添加
FrmAddClient frmAdd = new FrmAddClient();
frmAdd.MdiParent = this;
frmAdd.Show();
}
private void 客户查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
//查询客户
FrmSelectClient frmSelect = new FrmSelectClient();
frmSelect.MdiParent = this;
frmSelect.Show();
}
private void 客户查询ToolStripMenuItem1_Click(object sender, EventArgs e)
{
//查询客户
FrmSelectClient frmSelect = new FrmSelectClient();
frmSelect.MdiParent = this;
frmSelect.Show();
}
private void 客户添加ToolStripMenuItem1_Click(object sender, EventArgs e)
{
//客户添加
FrmAddClient frmAdd = new FrmAddClient();
frmAdd.MdiParent = this;
frmAdd.Show();
}
//退出所有程序
private void FrmSalesManage_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
private void 安全退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 安全退出ToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
查询窗体:“客户查询”代码:
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;
namespace product6._10
{
public partial class FrmSelectClient : Form
{
public FrmSelectClient()
{
InitializeComponent();
}
//刷新列表
public void RefreshList()
{
string sql = "select * from Customer";
DataTable table = DBHelper.GetDataTable(sql);
ClientInformation.DataSource = table;
}
//初始换程序选项
private void FrmSelectClient_Load(object sender, EventArgs e)
{
RefreshList();
cbType.Items.Clear();
cbType.Items.Add("--请选择--");
cbType.Items.Add("按姓名");
cbType.Items.Add("按电话");
cbType.Items.Add("按地址");
cbType.Items.Add("按编号");
cbType.SelectedIndex = 0;
}
//完善搜索信息
private void btSelect_Click(object sender, EventArgs e)
{
string key = cbCondition.Text;
if(cbType.SelectedIndex == 0)
{
//没有确切客户信息,则全能查找
string sql = string.Format("select * from Customer where Custname like '%{0}%' or CustID like '%{0}%'or Custtel like '%{0}%'or CustAddress like '%{0}%'or CustSex like '%{0}%'",key);
DataTable table = DBHelper.GetDataTable(sql);
ClientInformation.DataSource = table;
if (table.Rows.Count == 0)
{
MessageBox.Show(string.Format("没有找到与{0}相关的联系人", key));
}
}
//查无此人报错
if (cbType.SelectedIndex == 1)
{
//根据姓名查询客户
string sql = string.Format("select * from Customer where Custname like '%{0}%'",key);
DataTable table = DBHelper.GetDataTable(sql);
ClientInformation.DataSource = table;
if(table.Rows.Count == 0)
{
MessageBox.Show(string.Format("没有找到与{0}相关的联系人",key));
}
}else if(cbType.SelectedIndex == 2)
{
//根据电话查询客户
string sql = string.Format("select * from Customer where CustTel like '%{0}%'", key);
DataTable table = DBHelper.GetDataTable(sql);
ClientInformation.DataSource = table;
if (table.Rows.Count == 0)
{
MessageBox.Show(string.Format("没有找到与{0}相关的联系人", key));
}
}
else if(cbType.SelectedIndex == 3)
{
//根据地质查询客户
string sql = string.Format("select * from Customer where CustAddress like '%{0}%'",key);
DataTable table = DBHelper.GetDataTable(sql);
ClientInformation.DataSource = table;
if (table.Rows.Count == 0)
{
MessageBox.Show(string.Format("没有找到与{0}相关的联系人", key));
}
}
else if(cbType.SelectedIndex == 4)
{
//根据客户编号查询客户
string sql = string.Format("select * from Customer where CustID like '%{0}%'", key);
DataTable table = DBHelper.GetDataTable(sql);
ClientInformation.DataSource = table;
if (table.Rows.Count == 0)
{
MessageBox.Show(string.Format("没有找到与{0}相关的联系人", key));
}
}
}
//异窗口传参数
public static string id = "0";
public static string name;
public static string tel;
public static string sex;
public static string address;
private void ClientInformation_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
id = ClientInformation.SelectedRows[0].Cells[0].Value.ToString();
name = ClientInformation.SelectedRows[0].Cells[1].Value.ToString();
tel = ClientInformation.SelectedRows[0].Cells[2].Value.ToString();
sex = ClientInformation.SelectedRows[0].Cells[3].Value.ToString();
address = ClientInformation.SelectedRows[0].Cells[4].Value.ToString();
}
//弹出修改客户信息窗口
private void btAddClient_Click(object sender, EventArgs e)
{
//选中客户失败
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(tel) || string.IsNullOrEmpty(sex))
{
MessageBox.Show(string.Format("未选中唯一联系人,\n请重新单击选择客户"));
return;
}
FrmAmendClient frmAmend = new FrmAmendClient();
frmAmend.ShowDialog();
RefreshList();
}
//删除客户
private void buDeleteClient_Click(object sender, EventArgs e)
{
id = ClientInformation.SelectedRows[0].Cells[0].Value.ToString();
name = ClientInformation.SelectedRows[0].Cells[1].Value.ToString();
//给一个删除客户提示
string question = string.Format("你确定要将客户{0}拉入黑名单吗", name);
DialogResult result = MessageBox.Show(question,"删除提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(result == DialogResult.No)
{
return;
}
string sql = string.Format("delete from Customer where CustID = {0}",int.Parse(id));
if (DBHelper.ExecuteNonQuery(sql))
{
MessageBox.Show(string.Format("客户{0}已被您从名单中拉黑",name));
}
else
{
MessageBox.Show(string.Format("客户{0}删除失败,请向管理员核实",name));
}
}
//点击刷新列表
private void button1_Click(object sender, EventArgs e)
{
RefreshList();
}
}
}
添加窗体,“添加用户”代码:
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;
namespace product6._10
{
public partial class FrmAddClient : Form
{
public FrmAddClient()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//添加一个新客户,完善客户信息
string name = textName.Text;
string tel = textTel.Text;
string sex = "";
if(radMan.Checked == true)
{
sex = radMan.Text;
}else if(radWoman.Checked == true)
{
sex = radWoman.Text;
}
string address = textAddress.Text;
if(string.IsNullOrEmpty(name)|| string.IsNullOrEmpty(tel) || string.IsNullOrEmpty(sex))
{
MessageBox.Show("请完善客户信息");
return;
}
string sql = string.Format("insert into Customer values('{0}','{1}','{2}','{3}')",name,tel,sex,address);
if (DBHelper.ExecuteNonQuery(sql))
{
MessageBox.Show(string.Format("新客户{0}添加成功",name));
textName.Text = "";
textTel.Text = "";
radMan.Checked = false;
radWoman.Checked = false;
textAddress.Text = "";
}
else
{
MessageBox.Show(string.Format("新客户{0}添加失败,请向管理员反映情况",name));
}
}
//取消添加客户
private void button2_Click(object sender, EventArgs e)
{
//如果信息为空,直接关闭窗口
if(string.IsNullOrEmpty(textName.Text) && string.IsNullOrEmpty(textTel.Text)&&radMan.Checked == false && radWoman.Checked == false && string.IsNullOrEmpty(textAddress.Text))
{
Close();
}
//信息不为空,清空信息
textName.Text = "";
textTel.Text = "";
radMan.Checked = false;
radWoman.Checked = false;
textAddress.Text = "";
}
//姓名非空*号提示
private void textName_TextChanged(object sender, EventArgs e)
{
string name = textName.Text;
if (string.IsNullOrEmpty(name))
{
labName.Text = "*";
labName.ForeColor = Color.Red;
}
else
{
labName.Text = "";
}
}
//电话非空*号提示
private void textTel_TextChanged(object sender, EventArgs e)
{
string tel = textTel.Text;
if (string.IsNullOrEmpty(tel))
{
labTel.Text = "*";
labName.ForeColor = Color.Red;
}
else
{
labTel.Text = "";
}
}
//性别非空*号提示——男
private void radMan_CheckedChanged(object sender, EventArgs e)
{
if(radMan.Checked == false && radWoman.Checked == false)
{
labSex.Text = "*";
labSex.ForeColor = Color.Red;
}
else
{
labSex.Text = "";
}
}
//性别非空*号提示——女
private void radWoman_CheckedChanged(object sender, EventArgs e)
{
if (radMan.Checked == false && radWoman.Checked == false)
{
labSex.Text = "*";
labSex.ForeColor = Color.Red;
}
else
{
labSex.Text = "";
}
}
}
}
修改信息,“修改客户信息”代码:
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;
namespace product6._10
{
public partial class FrmAmendClient : Form
{
public FrmAmendClient()
{
InitializeComponent();
}
//非空验证提示
private void textName_TextChanged(object sender, EventArgs e)
{
string name = textName.Text;
if(string.IsNullOrEmpty(name))
{
labName.ForeColor = Color.Red;
labName.Text = "*";
}
else
{
labName.Text = "";
}
}
private void textTel_TextChanged(object sender, EventArgs e)
{
string tel = textTel.Text;
if (string.IsNullOrEmpty(tel))
{
labTel.ForeColor = Color.Red;
labTel.Text = "*";
}
else
{
labTel.Text = "";
}
}
private void radMan_CheckedChanged(object sender, EventArgs e)
{
if(radMan.Checked == false && radWoman.Checked == false)
{
labSex.Text = "*";
labSex.ForeColor = Color.Red;
}
else
{
labSex.Text = "";
}
}
private void radWoman_CheckedChanged(object sender, EventArgs e)
{
if (radMan.Checked == false && radWoman.Checked == false)
{
labSex.Text = "*";
labSex.ForeColor = Color.Red;
}
else
{
labSex.Text = "";
}
}
//确认修改客户信息系
private void button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(labID.Text);
string name = textName.Text;
string tel = textTel.Text;
string sex = "男";
if (radMan.Checked)
{
sex = radMan.Text;
}else if (radWoman.Checked)
{
sex = radWoman.Text;
}
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(tel) || string.IsNullOrEmpty(sex))
{
MessageBox.Show("请完善用户信息");
return;
}
string address = textAddress.Text;
string sql = string.Format("update Customer set Custname = '{0}',Custtel = '{1}',Custsex = '{2}',Custaddress = '{3}' where Custid = {4}",name,tel,sex,address,id);
if (DBHelper.ExecuteNonQuery(sql))
{
MessageBox.Show(string.Format("编号为{0}的客户信息修改成功",id));
this.Close();
}
else
{
MessageBox.Show(string.Format("编号为{0}的客户信息修改失败,请向管理员反映情况",id));
}
}
//自动根据选中异窗体传输的信息填写文本信息
private void FrmAmendClient_Load(object sender, EventArgs e)
{
labID.Text = FrmSelectClient.id;
textName.Text = FrmSelectClient.name;
textTel.Text = FrmSelectClient.tel;
if (FrmSelectClient.sex.Equals("男"))
{
radMan.Checked = true;
}else if (FrmSelectClient.sex.Equals("女"))
{
radWoman.Checked = true;
}
textAddress.Text = FrmSelectClient.address;
}
//保存后关闭窗口
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
更多推荐
C#可视化编程技术总结:制作可视化窗体软件
发布评论