-----------------------------------------------------体检项目类-----------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SJ.entity 8 { 9 /// <summary> 10 /// 体检项目 11 /// </summary> 12 public class HealthItems 13 { 14 /// <summary> 15 /// 体检项目名称 16 /// </summary> 17 public string Name { get; set; } 18 /// <summary> 19 /// 项目价格 20 /// </summary> 21 public int Price { get; set; } 22 /// <summary> 23 /// 项目描述 24 /// </summary> 25 public string Description { get; set; } 26 } 27 }
-----------------------------------------------------体检套餐类-----------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SJ.entity 8 { 9 /// <summary> 10 /// 体检套餐 11 /// </summary> 12 public class HealthCombos 13 { 14 /// <summary> 15 /// 套餐体检项目 16 /// </summary> 17 public List<HealthItems> Items { get; set; } 18 /// <summary> 19 /// 套餐价格 20 /// </summary> 21 public int Price { get; set; } 22 /// <summary> 23 /// 套餐名字 24 /// </summary> 25 public string Name { get; set; } 26 /// <summary> 27 /// 计算套餐价格 28 /// </summary> 29 /// <param name="items"></param> 30 31 public void CountPrice() 32 { 33 this.Price = 0; 34 if (Items != null) 35 { 36 foreach (var item in Items) 37 { 38 this.Price += item.Price; 39 } 40 } 41 } 42 43 public HealthCombos() 44 { 45 Items = new List<HealthItems>(); 46 } 47 } 48 }
-----------------------------------------------------主窗体-----------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using SJ.entity; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace SJ 13 { 14 public partial class frmMain : Form 15 { 16 public frmMain() 17 { 18 InitializeComponent(); 19 } 20 21 /// <summary> 22 /// 所有体检项目 23 /// </summary> 24 public List<HealthItems> allItems = new List<HealthItems>(); 25 //public Dictionary<string, HealthItems> allItems = new Dictionary<string, HealthItems>(); 26 //allItems.Add(defi.Name, defi); 27 //allItems.Add(hi1.Name,hi1); 28 //allItems.Add(hi2.Name, hi2); 29 //allItems.Add(hi3.Name, hi3); 30 //allItems.Add(hi4.Name,hi4); 31 //allItems.Add(hi5.Name, hi5); 32 //allItems.Add(hi6.Name, hi6); 33 34 /// <summary> 35 /// 套餐 36 /// </summary> 37 public Dictionary<string, HealthCombos> Combos = new Dictionary<string, HealthCombos>(); 38 39 /// <summary> 40 ///添加所有体检项目 41 /// </summary> 42 public void InitItems() 43 { 44 HealthItems defi = new HealthItems(); 45 defi.Name = "请选择"; 46 47 HealthItems hi1 = new HealthItems(); 48 hi1.Name = "身高"; 49 hi1.Price = 5; 50 hi1.Description = "用于检查身高"; 51 HealthItems hi2 = new HealthItems(); 52 hi2.Name = "体重"; 53 hi2.Price = 5; 54 hi2.Description = "用于检查体重"; 55 HealthItems hi3 = new HealthItems(); 56 hi3.Name = "视力"; 57 hi3.Price = 10; 58 hi3.Description = "用于检查视力"; 59 HealthItems hi4 = new HealthItems(); 60 hi4.Name = "听力"; 61 hi4.Price = 8; 62 hi4.Description = "用于检查听力"; 63 HealthItems hi5 = new HealthItems(); 64 hi5.Name = "肝功能"; 65 hi5.Price = 50; 66 hi5.Description = "用于检查肝功能"; 67 HealthItems hi6 = new HealthItems(); 68 hi6.Name = "B超"; 69 hi6.Price = 33; 70 hi6.Description = "用于检查B超"; 71 allItems.Add(defi); 72 allItems.Add(hi1); 73 allItems.Add(hi2); 74 allItems.Add(hi3); 75 allItems.Add(hi4); 76 allItems.Add(hi5); 77 allItems.Add(hi6); 78 } 79 80 /// <summary> 81 /// 初始入学套餐体检项 82 /// </summary> 83 public void InitSchoolCombos() 84 { 85 List<HealthItems> schoolCombos = new List<HealthItems>(); 86 foreach (var item in allItems) 87 { 88 if (item.Name.Equals("身高") || item.Name.Equals("肝功能") || item.Name.Equals("体重")) 89 { 90 schoolCombos.Add(item); 91 } 92 } 93 94 95 HealthCombos hc = new HealthCombos(); 96 hc.Items = schoolCombos; 97 hc.Name = "入学体检"; 98 99 HealthCombos defhc = new HealthCombos(); 100 defhc.Name = "请选择"; 101 Combos.Add(defhc.Name, defhc); 102 Combos.Add(hc.Name, hc); 103 } 104 105 /// <summary> 106 /// 绑定组合框数据源 107 /// </summary> 108 public void BangCMB() 109 { 110 //套餐列表 111 BindingSource bs = new BindingSource(); 112 bs.DataSource = Combos.Values; 113 cmbTaoCanList.DataSource = bs; 114 cmbTaoCanList.DisplayMember = "Name"; 115 116 //体检项目 117 cmbCheckItem.DataSource = allItems; 118 cmbCheckItem.DisplayMember = "Name"; 119 } 120 /// <summary> 121 /// 加载事件 122 /// </summary> 123 /// <param name="sender"></param> 124 /// <param name="e"></param> 125 private void frmMain_Load(object sender, EventArgs e) 126 { 127 InitItems(); 128 InitSchoolCombos(); 129 BangCMB(); 130 } 131 132 /// <summary> 133 /// 更改体检套餐,同时修改dgv绑定数据源 134 /// </summary> 135 /// <param name="sender"></param> 136 /// <param name="e"></param> 137 private void cmbTaoCanList_SelectedIndexChanged(object sender, EventArgs e) 138 { 139 if (!cmbTaoCanList.Text.Equals("SJ.entity.HealthCombos")) 140 { 141 BangDgvList(); 142 //更改“删除”按键可否使用属性 143 BtnIsOK(); 144 } 145 } 146 147 /// <summary> 148 /// 更改label 149 /// </summary> 150 public void ChangeLBL() 151 { 152 lblTaoCanName.Text = cmbTaoCanList.Text; 153 lblPrice.Text = Combos[cmbTaoCanList.Text].Price.ToString(); 154 } 155 156 public void BtnIsOK() 157 { 158 if (dgvList.SelectedRows.Count > 0) 159 { 160 btnDelItem.Enabled = true; 161 } 162 else 163 { 164 btnDelItem.Enabled = false; 165 } 166 if (cmbCheckItem.SelectedIndex == 0 || cmbTaoCanList.SelectedIndex == 0) 167 { 168 btnAddItem.Enabled = false; 169 } 170 else 171 { 172 btnAddItem.Enabled = true; 173 } 174 } 175 176 /// <summary> 177 /// 修改dgv绑定数据源 178 /// </summary> 179 public void BangDgvList() 180 { 181 BindingSource bs = new BindingSource(); 182 bs.DataSource = Combos[cmbTaoCanList.Text].Items; 183 dgvList.DataSource = bs; 184 CombosPrice(); 185 ChangeLBL(); 186 } 187 188 public void CombosPrice() 189 { 190 foreach (var item in Combos) 191 { 192 if (item.Key.Equals(cmbTaoCanList.Text)) 193 { 194 item.Value.CountPrice(); 195 break; 196 } 197 } 198 } 199 200 /// <summary> 201 /// 单击删除 202 /// </summary> 203 /// <param name="sender"></param> 204 /// <param name="e"></param> 205 private void btnDelItem_Click(object sender, EventArgs e) 206 { 207 HealthCombos c = Combos[cmbTaoCanList.Text]; 208 //遍历选中套餐名称的体检项 209 foreach (var item in Combos[cmbTaoCanList.Text].Items) 210 { 211 //当选中项目名称 匹配到 体检项里的名称时删除该泛型里的该项 212 if (item.Name.Equals(dgvList.SelectedRows[0].Cells[0].Value.ToString())) 213 { 214 MessageBox.Show("删除成功!!"); 215 Combos[cmbTaoCanList.Text].Items.Remove(item); 216 //重新绑定数据源 217 BangDgvList(); 218 break; 219 } 220 } 221 } 222 223 /// <summary> 224 /// 单击添加 225 /// </summary> 226 /// <param name="sender"></param> 227 /// <param name="e"></param> 228 private void btnAddItem_Click(object sender, EventArgs e) 229 { 230 //遍历判断选中的是哪个套餐 231 foreach (var combosItem in Combos) 232 { 233 if (combosItem.Key.Equals(cmbTaoCanList.Text)) 234 { 235 //遍历判断添加的项目是哪个 236 foreach (HealthItems aitem in allItems) 237 { 238 if (aitem.Name.Equals(cmbCheckItem.Text)) 239 { 240 //遍历判断该套餐里有无重复项目 241 if (combosItem.Value.Items != null) 242 { 243 foreach (var itemss in combosItem.Value.Items) 244 { 245 if (aitem.Name.Equals(itemss.Name)) 246 { 247 MessageBox.Show("已有该体检项目!!"); 248 return; 249 } 250 } 251 } 252 MessageBox.Show("添加成功!!"); 253 combosItem.Value.Items.Add(aitem); 254 BangDgvList(); 255 BtnIsOK(); 256 return; 257 } 258 } 259 } 260 } 261 } 262 263 /// <summary> 264 /// 改变选项 265 /// </summary> 266 /// <param name="sender"></param> 267 /// <param name="e"></param> 268 private void cmbCheckItem_SelectedIndexChanged(object sender, EventArgs e) 269 { 270 BtnIsOK(); 271 } 272 273 /// <summary> 274 /// 添加套餐 275 /// </summary> 276 /// <param name="sender"></param> 277 /// <param name="e"></param> 278 private void btnAddTaoCan_Click(object sender, EventArgs e) 279 { 280 string taoCanName = txtTaoCanName.Text; 281 HealthCombos hccc= new HealthCombos(); 282 hccc.Name = taoCanName; 283 foreach (var item in Combos) 284 { 285 if (item.Value.Name.Equals(taoCanName)) 286 { 287 MessageBox.Show("已有该套餐!!"); 288 return; 289 } 290 } 291 Combos.Add(hccc.Name, hccc); 292 MessageBox.Show("添加" + hccc.Name + "套餐成功!!"); 293 BangCMB(); 294 } 295 } 296 }