今天,我們來寫一個體檢套餐管理系統的小項目
項目需求:
1.加載默認體檢套餐,如下圖所示
2.顯示指定套餐的項目明細
3.向指定套餐添加檢查項目信息
4.刪除套餐中的項目信息
5.新建套餐
開始:
首先要做的就是搭好窗體
然後創建CheckItems類
public class CheckItems
{
public string description { get; set; }
public string name { get; set; }
public int price { get; set; }
public static List<CheckItems> list = new List<CheckItems>();
public static Dictionary<string, List<CheckItems>> dic = new Dictionary<string, List<CheckItems>>();
}
類中包含兩個集合
list存儲體檢項目
dic存儲體檢套餐
然後添加一些常見的體檢項目和一個基本套餐
CheckItems c1 = new CheckItems();
c1.name = "身高";
c1.price = 5;
c1.description = "用來檢查身高";
CheckItems c2 = new CheckItems();
c2.name = "體重";
c2.price = 5;
c2.description = "用來檢查體重";
CheckItems c3 = new CheckItems();
c3.name = "肝功能";
c3.price = 50;
c3.description = "用來檢查肝功能";
CheckItems c4 = new CheckItems();
c4.name = "視力";
c4.price = 10;
c4.description = "用來檢查視力";
CheckItems c5 = new CheckItems();
c5.name = "聽力";
c5.price = 10;
c5.description = "用來檢查聽力";
CheckItems c6 = new CheckItems();
c6.name = "B超";
c6.price = 30;
c6.description = "用來檢查B超";
CheckItems c7 = new CheckItems();
c7.name = "心電圖";
c7.price = 50;
c7.description = "用來檢查心電圖";
CheckItems c8 = new CheckItems();
c8.name = "血壓";
c8.price = 20;
c8.description = "用來檢查血壓";
CheckItems c9 = new CheckItems();
c9.name = "血常規";
c9.price = 20;
c9.description = "用來檢查血常規";
CheckItems.list.Add(c1);
CheckItems.list.Add(c2);
CheckItems.list.Add(c3);
CheckItems.list.Add(c4);
CheckItems.list.Add(c5);
CheckItems.list.Add(c6);
CheckItems.list.Add(c7);
CheckItems.list.Add(c8);
CheckItems.list.Add(c9);
for (int i = 0; i < CheckItems.list.Count; i++)
{
comboBox2.Items.Add(CheckItems.list[i].name);
}
List<CheckItems> li = new List<CheckItems>();
li.Add(c1);
li.Add(c2);
li.Add(c3);
CheckItems.dic.Add("入學體檢", li);
點擊添加可以添加一個體檢套餐的集合
List<CheckItems> li = new List<CheckItems>();
string name = textBox1.Text;
CheckItems.dic.Add(name, li);
comboBox1.Items.Add(textBox1.Text);
MessageBox.Show("添加成功!");
點擊體檢套餐維護下的添加可以為comboBox名字的套餐內添加檢查項目,並且會自動加載價格
string name = comboBox1.Text;
List<CheckItems> li = CheckItems.dic[name];
foreach (CheckItems c in CheckItems.list)
{
if (c.name == comboBox2.Text)
{
li.Add(c);
sum += c.price;
MessageBox.Show("添加成功!");
break;
}
}
label7.Text = sum.ToString();
this.dataGridView1.DataSource = new BindingList<CheckItems>(CheckItems.dic[comboBox1.Text]);
刪除也是一樣的,可以刪除套餐內所選定的體檢項目
string name = comboBox1.Text;
List<CheckItems> li = CheckItems.dic[name];
string str = "";
foreach (CheckItems c in CheckItems.list)
{
str = dataGridView1.SelectedRows[0].Cells["Column1"].Value.ToString();
if (c.name ==str )
{
li.Remove(c);
sum -= c.price;
MessageBox.Show("刪除成功!");
break;
}
}
label7.Text = sum.ToString();
this.dataGridView1.DataSource = new BindingList<CheckItems>(CheckItems.dic[comboBox1.Text]);
最後一步就是動態加載dataGrideView
其實很簡單,只要把事件寫在comboBox的comboBox1_SelectedIndexChanged事件下就可以了
label5.Text = comboBox1.Text;
List<CheckItems> li = new List<CheckItems>();
li = CheckItems.dic[comboBox1.Text];
foreach (CheckItems c in li)
{
sum += c.price;
}
label7.Text = sum.ToString();this.dataGridView1.DataSource = new BindingList<CheckItems>(CheckItems.dic[comboBox1.Text]);
這樣,一個簡單的體檢套餐小項目就OVER了