C# ComboBox的聯(lián)動(dòng)操作(三層架構(gòu))
項(xiàng)目需求:根據(jù)年級(jí)下拉框的變化使得科目下拉框綁定次年級(jí)下對(duì)應(yīng)有的值
我們用三層架構(gòu)的模式來實(shí)現(xiàn)
1.我們想和數(shù)據(jù)庫交互,我們首先得來先解決DAL數(shù)據(jù)庫交互層
01.獲得年級(jí)下拉框的數(shù)據(jù)
在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 { //數(shù)據(jù)訪問層 public class GradeDAL { public static string Constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; #region 獲得年級(jí)表 public DataTable SelectGrade(string gradetype) { //和數(shù)據(jù)庫交互 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); } //返回一張表的數(shù)據(jù) return ds.Tables["stuInfo"]; } #endregion #region 獲取年級(jí)數(shù)據(jù),為在下拉框中顯示 //定義一個(gè)集合,儲(chǔ)存年級(jí)信息 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) { //每一個(gè)row代表表中的一行,所以一行對(duì)應(yīng)一個(gè)年級(jí)對(duì)象 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的作用可以釋放資源,利于資源的回收(可以省略關(guān)閉連接) 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.在業(yè)務(wù)邏輯層
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 獲取年級(jí)數(shù)據(jù),為在下拉框中顯示 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事件中加載年級(jí)下拉框
private void FrmSelectResult_Load(object sender, EventArgs e) { #region 加載年級(jí)下拉框 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 }
其中在使用
獲得年級(jí)下拉框隱藏值得方法(2)
int num = Convert.ToInt32(cboGrade.SelectedValue);
加載年級(jí)下拉框時(shí):會(huì)出現(xiàn)的錯(cuò)誤的寫法
把cboGrade.DataSource = list;寫在
cboGrade.ValueMember = "GradeId";
cboGrade.DisplayMember = "GradeName";上面
即:
#region 加載年級(jí)下拉框 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
這是就會(huì)出現(xiàn)下面錯(cuò)誤:
在年級(jí)的SelectedIndexChanged事件中
try { //根據(jù)年級(jí)取得科目信息并綁定 #region 獲得年級(jí)下拉框隱藏值得方法(1) Grade sub = (Grade)cboGrade.SelectedItem; int num =sub.GradeId; #endregion #region 獲得年級(jí)下拉框隱藏值得方法(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("出錯(cuò)"); }
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)C#程序設(shè)計(jì)有所幫助。
- C# ComboBox控件“設(shè)置 DataSource 屬性后無法修改項(xiàng)集合”的完美解決方法
- C#實(shí)現(xiàn)帶搜索功能的ComboBox
- C# 重寫ComboBox實(shí)現(xiàn)下拉任意組件的方法
- C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
- C#實(shí)現(xiàn)綁定Combobox的方法
- C#用ComboBox控件實(shí)現(xiàn)省與市的聯(lián)動(dòng)效果的方法
- C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)
- C# listview添加combobox到單元格的實(shí)現(xiàn)代碼
- c#構(gòu)造ColorComboBox(顏色下拉框)
- C#實(shí)現(xiàn)ComboBox自動(dòng)匹配字符
- C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
相關(guān)文章
淺析C#?AsyncLocal如何在異步間進(jìn)行數(shù)據(jù)流轉(zhuǎn)
在異步編程中,處理異步操作之間的數(shù)據(jù)流轉(zhuǎn)是一個(gè)比較常用的操作,C#異步編程提供了一個(gè)強(qiáng)大的工具來解決這個(gè)問題,那就是AsyncLocal,下面我們就來看看AsyncLocal的原理和用法吧2023-08-08c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼
這篇文章主要介紹了c# socket網(wǎng)絡(luò)編程,server端接收,client端發(fā)送數(shù)據(jù),大家參考使用吧2013-12-12C#和JavaScript實(shí)現(xiàn)交互的方法
最近做一個(gè)小項(xiàng)目不可避免的需要前端腳本與后臺(tái)進(jìn)行交互。由于是在asp.net中實(shí)現(xiàn),故問題演化成asp.net中jiavascript與后臺(tái)c#如何進(jìn)行交互。2015-05-05利用C#實(shí)現(xiàn)將小數(shù)值四舍五入為整數(shù)
在項(xiàng)目的開發(fā)中,遇到一些除法計(jì)算內(nèi)容會(huì)產(chǎn)生小數(shù)值,但是又需要根據(jù)項(xiàng)目的實(shí)際情況將這些小數(shù)內(nèi)容化為整數(shù),所以本文為大家整理了C#實(shí)現(xiàn)將小數(shù)值四舍五入為整數(shù)的方法,希望對(duì)大家有所幫助2023-07-07c#遠(yuǎn)程html數(shù)據(jù)抓取實(shí)例分享
這篇文章主要介紹了c#遠(yuǎn)程html數(shù)據(jù)抓取的方法,大家參考使用吧2013-12-12