步骤:
定义几个类:
HealthCheckItem类:检查项目
属性:
public string Description { get; set; }public int Price { get; set; }public string Name { get; set; }
HealthCheckItem类中的方法:
//当选择套餐下拉框中的套餐时,套餐下所有检查项目都添加到dgvlist中显示
public HealthCheckItem(string name, int price, stringdescription)
{
Name=name;
Price=price;
Description=description;
}
主界面:
//定义多个检查项目
HealthCheckItem m,hg, wg, sg, hr, lf, eg, ba, bp, bt;//定义系统默认检查套餐"入学体检";
HealthCheckSet setA;//采用泛型集合List保存所有的体检项目
List allitems = new List();//采用泛型集合List保存套餐中的体检项目
List items = new List();//使用双列集合(字典)保存套餐集合
public Dictionary hs = new Dictionary();
HealthCheckSet类:体检套餐
属性:
public int Price { get; set; }//Item属性中检查项目的价格之和
public string Name { get; set; }public List Items { get; set; }//Items是HealthCheckItem的集合,
HealthCheckSet类中的方法:
publicHealthCheckSet()
{
Items= new List();
}public HealthCheckSet(string name, Listitems)
{
Name=name;
Items=items;
}
获取价格的方法:
//获取价格的方法
public voidCalcPrice()
{int tatolPrice = 0;foreach (HealthCheckItem item inItems)
{
tatolPrice= tatolPrice +item.Price;
}//套餐的价格等于每个体检项目价格的和
Price =tatolPrice;
}
添加新套餐:
//添加新套餐
private void btnAdd_Click(objectsender, EventArgs e)
{if (txtName.Text.Equals(""))
{
MessageBox.Show("请输入套餐名称!");
}else{
HealthCheckSet hc= newHealthCheckSet();
hs.Add(txtName.Text, hc);//调加载套餐下拉框信息方法
InitiateHealthSetList();
cboList.SelectedIndex=hs.Count();
lblname.Text=cboList.Text;
hc.Name=cboList.Text;
MessageBox.Show("添加成功!");
}
}
加载套餐下拉框信息:
private voidInitiateHealthSetList()
{//加载套餐下拉框信息//先清空下拉框列表
cboList.Items.Clear();//添加请选择
cboList.Items.Add("请选择");//将dictionary的key值绑定到下拉框中,作为下拉框显示的值
foreach (string k inhs.Keys)
{
cboList.Items.Add(k);
}//默认第一项被选中
cboList.SelectedIndex = 0;
}
初始化检查项目:
hg = new HealthCheckItem("身高", 15, "用于检查身高");
wg= new HealthCheckItem("体重", 25, "用于检查体重");
allitems.Add(hg);
allitems.Add(wg);
添加检查项目:
//添加检查项目
private void btnOk_Click(objectsender, EventArgs e)
{if (cboProject.Text.Equals("请选择")||cboProject.Text.Equals(""))
{
MessageBox.Show("请选择项目");return;
}if (cboList.Text == "请选择")
{
MessageBox.Show("请选择套餐");return;
}//List.Contains(对象)可以判断某个对象是否在集合中
if (!hs[cboList.Text].Items.Contains(allitems[cboProject.SelectedIndex]))
{//添加检查项目 hs[cboList.Text].Items.Add(allitems[cboProject.SelectedIndex]);//重新计算总价格
hs[cboList.Text].CalcPrice();//更新
UpdateSet(hs[cboList.Text]);//刷新窗体集合名称
lblname.Text =hs[cboList.Text].Name;// //刷新窗体集合价格
lblprice.Text =hs[cboList.Text].Price.ToString();
MessageBox.Show("添加成功");
}else{
MessageBox.Show("该项目已经存在");
}
}
删除体检套餐信息:
//删除信息
private void btnDelete_Click(objectsender, EventArgs e)
{if (this.dgvInfo.SelectedRows.Count == 0)
{
MessageBox.Show("请选择要删除的一行");return;
}//找索引
int index = dgvInfo.SelectedRows[0].Index;//删除的检查项目数据//泛型集合删除项目的方法:RemoveAt();
hs[cboList.Text].Items.RemoveAt(index);//重新计算价格
hs[cboList.Text].CalcPrice();//更新dgvlist数据
UpdateSet(hs[cboList.Text]);
lblname.Text=setA.Name;string choS =cboList.Text;
lblprice.Text=hs[choS].Price.ToString();
}
填充套餐的dgvlist,更新套餐检查项目
//填充套餐的dgvlist,更新套餐检查项目
private void UpdateSet(HealthCheckSet set)
{
dgvInfo.DataSource= new BindingList(set.Items);
}
这样简单明了-----------------------------------