ComBox联动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36024699/article/details/68490150


public class StudentDAL  
    {  
        public List<Grade> SelectGradeName()  
        {  
            string str = SqlHelper.Constr;          
            string sql = "select * from grade";  
            List<Grade> glist = new List<Grade>();  
            using (SqlConnection con = new SqlConnection(str))  
            {  
                SqlCommand cmd = new SqlCommand(sql,con);  
                con.Open();  
                SqlDataReader dr = cmd.ExecuteReader();  
                while (dr.Read())  
                {  
                    Grade item = new Grade();  
                    item.GradeId = Convert.ToInt32(dr["gradeid"]);  
                    item.GradeName = dr["gradename"].ToString();  
                    glist.Add(item);  
                }  
                dr.Close();  
                return glist;  
            }  
         }  
          
        public List<Subject> SelectSubjectName(string name)  
        {  
            string str = SqlHelper.Constr;  
            string sql = "select * from subject,grade where grade.gradeid=subject.gradeid and gradename='"+name+"'";  
            List<Subject> slist = new List<Subject>();  
            using (SqlConnection con = new SqlConnection(str))  
            {  
                SqlCommand cmd = new SqlCommand(sql, con);  
                con.Open();  
                SqlDataReader dr = cmd.ExecuteReader();  
                while (dr.Read())  
                {  
                    Subject item = new Subject();  
                    item.SubjectId = Convert.ToInt32(dr["subjectid"]);  
                    item.SubjectName = dr["subjectname"].ToString();  
                    item.ClassHour = Convert.ToInt32(dr["classhour"]);  
                    item.GradeId = Convert.ToInt32(dr["gradeid"]);  
                    slist.Add(item);  
                }  
                dr.Close();  
                return slist;  
            }  
        }  
        public List<Subject> SelectSubjectName()  
        {  
            string str = SqlHelper.Constr;  
            string sql = "select * from subject";  
            List<Subject> slist = new List<Subject>();  
            using (SqlConnection con = new SqlConnection(str))  
            {  
                SqlCommand cmd = new SqlCommand(sql, con);  
                con.Open();  
                SqlDataReader dr = cmd.ExecuteReader();  
                while (dr.Read())  
                {  
                    Subject item = new Subject();  
                    item.SubjectId = Convert.ToInt32(dr["subjectid"]);  
                    item.SubjectName = dr["subjectname"].ToString();  
                    item.ClassHour = Convert.ToInt32(dr["classhour"]);  
                    item.GradeId = Convert.ToInt32(dr["gradeid"]);  
                    slist.Add(item);  
                }  
                dr.Close();  
                return slist;  
            }  
        }  
  
        public DataTable FillListView(string name,string grade,string subject)  
        {  
            string str = SqlHelper.Constr;  
            using(SqlConnection con = new SqlConnection(str)){  
            string sql = "select studentname as '学生姓名',subjectname as '科目',studentresult as '成绩',examdate as '考试时间' from result inner join student on student.studentno=result.studentno inner join subject on subject.subjectid=result.subjectid inner join grade on grade.gradeid=student.gradeid where studentname like '%" + name + "%' and gradename='"+grade+"' and subjectname='"+subject+"'";  
            con.Open();  
            SqlDataAdapter da = new SqlDataAdapter();  
            SqlCommand cmd = new SqlCommand(sql, con);  
            da.SelectCommand = cmd;  
            DataSet ds = new DataSet();  
            da.Fill(ds, "Student");  
            con.Close();  
            return ds.Tables["Student"];  
            }  
        }  
    }  

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Drawing;    
  6. using System.Linq;    
  7. using System.Text;    
  8. using System.Threading.Tasks;    
  9. using System.Windows.Forms;    
  10. using MySchoolDAL;    
  11. using MySchoolModels;    
  12.     
  13. namespace MySchoolUI    
  14. {    
  15.     public partial class StudentResult : Form    
  16.     {    
  17.         public StudentResult()    
  18.         {    
  19.             InitializeComponent();    
  20.         }    
  21.     
  22.         private void btnSearch_Click(object sender, EventArgs e)    
  23.         {    
  24.             StudentDAL sd = new StudentDAL();    
  25.             string s1 = this.comboBox1.Text;    
  26.             string s2 = this.comboBox2.Text;    
  27.             string name = this.txtStudentName.Text;    
  28.             DataTable dt = sd.FillListView(name,s1,s2);    
  29.             this.dataGridView1.DataSource = dt;    
  30.         }    
  31.             StudentDAL sd = new StudentDAL();    
  32.         private void StudentResult_Load(object sender, EventArgs e)    
  33.         {    
  34.             List<Grade> lg = sd.SelectGradeName();    
  35.             foreach (Grade item in lg)    
  36.             {    
  37.                 this.comboBox1.Items.Add(item.GradeName);    
  38.             }    
  39.             List<Subject> ls = sd.SelectSubjectName();    
  40.             foreach (Subject item in ls)    
  41.             {    
  42.                 this.comboBox2.Items.Add(item.SubjectName);    
  43.             }    
  44.         }    
  45.     
  46.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)    
  47.         {    
  48.             string s = this.comboBox1.Text;    
  49.             this.comboBox2.Items.Clear();    
  50.             List<Subject> ls = sd.SelectSubjectName(s);    
  51.             foreach (Subject item in ls)    
  52.             {    
  53.                 this.comboBox2.Items.Add(item.SubjectName);    
  54.             }    
  55.         }    
  56.     }    
  57. }   
展开阅读全文

没有更多推荐了,返回首页