HealthCheckItem类
public class HealthCheckItem { //带参构造 public HealthCheckItem(string name,string description, int price) { this.Description = description; this.Name = name; this.Price = price; } //无参构造 public HealthCheckItem() { } //项目描述 private string description; public string Description { get { return description; } set { description = value; } } //项目名称 private string name; public string Name { get { return name; } set { name = value; } } //项目价格 private int price; public int Price { get { return price; } set { price = value; } } }
HealthCheckSet类
public class HealthCheckSet { //带参构造 public HealthCheckSet(string name,Dictionary<string,HealthCheckItem> dicc) { } //无参构造 public HealthCheckSet() { } //套餐价格 private int price; public int Price { get { return price; } set { price = value; } } //套餐名称 private string name; public string Name { get { return name; } set { name = value; } } //保存套餐项目 Dictionary<string, HealthCheckItem> dic = new Dictionary<string, HealthCheckItem>(); public Dictionary<string, HealthCheckItem> Dic { get { return dic; } set { dic = value; } } //计算套餐价格方法 public int CalcPrice() { int num = 0; foreach (HealthCheckItem item in dic.Values) { num += item.Price; } this.price = num; return num; } }
在Load事件里写的代码:
private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.AutoGenerateColumns = false;//清除多余的列 projects(); InitHealth(); cboPackageAdd(); com2(); } //检查项目集合,身高,体重,视力,听力,肝功能,B超,心电图 public HealthCheckItem height, weight, Vision, hearing, liver, B_mode, electrocardiogram; Dictionary<string, HealthCheckItem> project = new Dictionary<string, HealthCheckItem>(); //初始化检查项目集合 public void projects() { height = new HealthCheckItem("身高", "用于检查身高.", 20); weight = new HealthCheckItem("体重", "用于检查体重.", 20); Vision = new HealthCheckItem("视力", "用于检查视力.", 20); hearing = new HealthCheckItem("听力", "用于检查听力.", 20); liver = new HealthCheckItem("肝功能", "用于检查肝功能.", 50); B_mode = new HealthCheckItem("B超", "用于检查B超.", 50); electrocardiogram = new HealthCheckItem("心电图", "用于检查心电图.", 60); project.Add(height.Name, height); project.Add(weight.Name, weight); project.Add(Vision.Name, Vision); project.Add(hearing.Name, hearing); project.Add(liver.Name, liver); project.Add(B_mode.Name, B_mode); project.Add(electrocardiogram.Name, electrocardiogram); } //用于保存套餐集合 Dictionary<string, HealthCheckSet> set = new Dictionary<string, HealthCheckSet>(); //用于保存入学体检初始化信息 Dictionary<string, HealthCheckItem> set1 = new Dictionary<string, HealthCheckItem>(); //初始化入学体检套餐 public void InitHealth() { set1.Add(height.Name, height); set1.Add(weight.Name, weight); HealthCheckSet s = new HealthCheckSet(); s.Name= "入学体检"; s.Dic = set1; set.Add("入学体检", s); } // 绑定套餐下拉框 public void cboPackageAdd() { comboBox1.Items.Clear(); comboBox1.Items.Add("请选择"); comboBox1.SelectedIndex = 0; foreach (HealthCheckSet item in set.Values) { comboBox1.Items.Add(item.Name); } } // 绑定检查项目的下拉框 public void com2() { comboBox2.Items.Add("请选择"); comboBox2.SelectedIndex = 0; foreach (string item in project.Keys) { comboBox2.Items.Add(item); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string name = comboBox1.Text;//获取套餐名称 if (comboBox1.Text == "请选择") { label5.Text = ""; label7.Text = ""; dataGridView1.DataSource = new BindingList<HealthCheckItem>(); return; } label5.Text = name; label7.Text = set[name].CalcPrice().ToString(); dataGridView1.DataSource = new BindingList<HealthCheckItem>(set[name].Dic.Values.ToList()); } public void Update(string Name) { dataGridView1.DataSource = new BindingList<HealthCheckItem>(set[Name].Dic.Values.ToList()); } private void button2_Click(object sender, EventArgs e) { string name = comboBox1.Text;//获取套餐名称 string name1 = comboBox2.Text;//获取项目项 if (set[name].Dic.ContainsKey(name1)) { MessageBox.Show("已添加"); return; } set[name].Dic.Add(name1, project[name1]); Update(name); label5.Text = name; label7.Text = set[name].CalcPrice().ToString(); } private void button3_Click(object sender, EventArgs e) { string name = comboBox1. Text;//获取套餐名称 string name1 = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); MessageBox.Show(name1); set[name].Dic.Remove(name1); Update(name); label7.Text = set[name].CalcPrice().ToString(); MessageBox.Show("删除成功"); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == string.Empty) { MessageBox.Show("不允许为空"); return; } HealthCheckSet kset = new HealthCheckSet(); kset.Name = textBox1.Text; set.Add(kset.Name, kset); MessageBox.Show("添加成功"); cboPackageAdd(); comboBox1.SelectedIndex = set.Count; } }