C# ComboBox的聯動操作(三層架構)。本站提示廣大學習愛好者:(C# ComboBox的聯動操作(三層架構))文章只能為提供參考,不一定能成為您想要的結果。以下是C# ComboBox的聯動操作(三層架構)正文
項目需求:依據年級下拉框的變更使得科面前目今拉框綁定次年級下對應有的值
我們用三層架構的形式來完成
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#法式設計有所贊助。