C# ComboBox的联动操作(三层架构)

内容摘要
项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值

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

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

01.获得年级
文章正文

项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值

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

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#程序设计有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!