这篇文章主要介绍了C# ComboBox的联动操作(三层架构),根据下拉框的变化使得下拉框绑定对应值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了C# ComboBox的联动操作(三层架构),根据下拉框的变化使得下拉框绑定对应值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了C# ComboBox的联动操作(三层架构),根据下拉框的变化使得下拉框绑定对应值,感兴趣的小伙伴们可以参考一下
项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值

我们用三层架构的模式来实现

1.我们想和数据库交互,我们首先得来先解决dal数据库交互层

01.获得年级下拉框的数据
在gradedal类中
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
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.在业务逻辑层

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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事件中加载年级下拉框
发表评论