C# ComboBox的联动操作(三层架构)
程序员文章站
2022-05-28 19:18:15
项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值
我们用三层架构的模式来实现
1.我们想和数据库交互,我们首先得来先解决dal数据库交互层...
项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值
我们用三层架构的模式来实现
1.我们想和数据库交互,我们首先得来先解决dal数据库交互层
01.获得年级下拉框的数据
在gradedal类中
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.data.sqlclient; using system.data; using myschool.model; using system.configuration; namespace myschool.dal { //数据访问层 public class gradedal { public static string constr = configurationmanager.connectionstrings["constr"].connectionstring; #region 获得年级表 public datatable selectgrade(string gradetype) { //和数据库交互 string str = "data source=.;initial catalog=myschool;uid=sa"; sqlconnection con = new sqlconnection(str); string sql = ""; if (gradetype=="") { sql = "select * from grade"; } else { sql = "select * from student where gradeid in (select gradeid from grade where gradename='" + gradetype + "')"; } sqldataadapter da = new sqldataadapter(sql, con); dataset ds = new dataset(); //捕获异常 try { da.fill(ds, "stuinfo"); } catch (exception ex) { throw new exception(ex.message); } //返回一张表的数据 return ds.tables["stuinfo"]; } #endregion #region 获取年级数据,为在下拉框中显示 //定义一个集合,储存年级信息 list<grade> list = new list<grade>(); #region 方法一: 以返回表的方式 public datatable loadcombox() { string sql = "select * from grade"; datatable dt = sqlhelper.executedatatable(sql); return dt; } #endregion #region 方法二:以返回集合的方式 public list<grade> loadcombox2() { string sql = "select * from grade"; datatable dt = sqlhelper.executedatatable(sql); //方法一: foreach (datarow row in dt.rows) { //每一个row代表表中的一行,所以一行对应一个年级对象 grade grade = new grade(); grade.gradeid = convert.toint32(row["gradeid"]); grade.gradename = row["gradename"].tostring(); list.add(grade); } //方法二:(使用mytool类) //mytool tool=new mytool(); //list = tool.datatabletolist<grade>(dt); return list; } #endregion #region 方法三:要求使用using语句 public list<grade> loadcombox3() { //using的作用可以释放资源,利于资源的回收(可以省略关闭连接) using (sqlconnection con=new sqlconnection(constr)) { try { string sql = "select * from grade"; sqlcommand cmd = new sqlcommand(sql,con); con.open(); sqldatareader dr = cmd.executereader(); while (dr.read()) { grade gr = new grade(); gr.gradeid = convert.toint32(dr["gradeid"]); gr.gradename=dr["gradename"].tostring(); list.add(gr); } } catch (exception ex) { throw new exception(ex.message); } } return list; } #endregion #endregion } }
02.在业务逻辑层
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using myschool.dal; using system.data; using myschool.model; namespace myschool.bll { public class gradebll { gradedal gradedal = new gradedal(); #region 获取年级数据,为在下拉框中显示 public datatable selectgrade(string gradetype) { return gradedal.selectgrade(gradetype); } public datatable loadcombox() { return gradedal.loadcombox(); } public list<grade> loadcombox2() { return gradedal.loadcombox2(); } #endregion public list<grade> loadcombox3() { return gradedal.loadcombox3(); } } }
03.在窗体ui层
在load事件中加载年级下拉框
private void frmselectresult_load(object sender, eventargs e) { #region 加载年级下拉框 try { list<grade> list = gradedal.loadcombox3(); list.insert(0, new grade() { gradeid=-1,gradename="--全部--" }); cbograde.valuemember = "gradeid"; cbograde.displaymember = "gradename"; cbograde.datasource = list; } catch (exception ex) { messagebox.show(ex.message); } #endregion #region 加载科目下拉框 //try //{ // list2 = subjectdal.loadcomboxsub(); // list2.insert(0, new subject() { subjectid = -1, subjectname = "--全部--" }); // cbosubject.valuemember = "subjectid"; // cbosubject.displaymember = "subjectname"; // cbosubject.datasource = list2; //} //catch (exception ex) //{ // messagebox.show(ex.message); //} #endregion }
其中在使用
获得年级下拉框隐藏值得方法(2)
int num = convert.toint32(cbograde.selectedvalue);
加载年级下拉框时:会出现的错误的写法
把cbograde.datasource = list;写在
cbograde.valuemember = "gradeid";
cbograde.displaymember = "gradename";上面
即:
#region 加载年级下拉框 try { list<grade> list = gradedal.loadcombox3(); list.insert(0, new grade() { gradeid=-1,gradename="--全部--" }); cbograde.datasource = list; cbograde.valuemember = "gradeid"; cbograde.displaymember = "gradename"; } catch (exception ex) { messagebox.show(ex.message); } #endregion
这是就会出现下面错误:
在年级的selectedindexchanged事件中
try { //根据年级取得科目信息并绑定 #region 获得年级下拉框隐藏值得方法(1) grade sub = (grade)cbograde.selecteditem; int num =sub.gradeid; #endregion #region 获得年级下拉框隐藏值得方法(2) // int num = convert.toint32(cbograde.selectedvalue.tostring()); #endregion list<subject> list = subjectdal.loadcomboxsub2(num); cbosubject.valuemember = "subjectid"; cbosubject.displaymember = "subjectname"; cbosubject.datasource = list; } catch (exception) { messagebox.show("出错"); }
以上就是本文的全部内容,希望对大家学习c#程序设计有所帮助。