using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class mainform : Form
    {
        public mainform()
        {
            InitializeComponent();
        }
        Mydatabase mydatabase = new Mydatabase();
        /// <summary>
        /// 加载初始化combobox数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainform_Load(object sender, EventArgs e)
        {
            //绑定任课教师信息
            DataTable dt = mydatabase.GetDataTable("select tno,tname from teacher");
            if (dt.Rows.Count>0)
            {
                this.comboBox1.DataSource = dt;
                comboBox1.DisplayMember = "tname";
                comboBox1.ValueMember = "tno";
            }

            //绑定学期信息
            string sql2 = "select distinct top 5 skxq,SUBSTRING(skxq,1,4)+'-'+cast((CAST (SUBSTRING(skxq,1,4) AS int)+1) as varchar(10))+'学年第'+SUBSTRING(skxq,5,1)+'学期' as skxqname  from tc order by skxq desc";
            DataTable dt1 = mydatabase.GetDataTable(sql2);
            if (dt1.Rows.Count > 0)
            {
                this.comboBox3.DataSource = dt1;
                comboBox3.DisplayMember = "skxqname";
                comboBox3.ValueMember = "skxq";
            }
        }
        /// <summary>
        /// 当教师信息发生变化时候，显示该任课教师的所有授课班级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //其次，绑定某教师授课的班级信息
            //comboBox2.Items.Clear();
            if (comboBox1.SelectedValue.ToString().Trim() != "")
            {
                DataRowView rowView = (DataRowView)comboBox1.SelectedItem;
                string tno = rowView["tno"].ToString();
                string sql3 = "select bno,bname from class where bno in(select bno from tc where tno=" + tno + ")";
                DataTable dt1 = mydatabase.GetDataTable(sql3);
                if (dt1.Rows.Count > 0)
                {
                    this.comboBox2.DataSource = dt1.DefaultView;
                    comboBox2.DisplayMember = "bname";
                    comboBox2.ValueMember = "bno";
                }
            }
        }
        
        /// <summary>
        /// 显示该教师的授课班级信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1 || comboBox3.SelectedIndex == -1)
            {
                MessageBox.Show("三项列表必须全部选择信息", "信息提示");
            }
            else
            {
                string tno = comboBox1.SelectedValue.ToString();
                string tclass = comboBox2.SelectedValue.ToString();
                string sxq = comboBox3.SelectedValue.ToString();

                //string sql = "select tcno,sktime from tc where tno=" + Convert.ToString(Session["userid"]) + " and cno in (select distinct cno from sc where isposted=1) and bno=" + DropBj.SelectedValue.ToString() + " order by tcno desc";
                string sql = string.Format("select tc.tcno,class.bname,tc.sktime,school.schname,course.cname,course.ctime from tc,class,school,course where tc.tno="+tno+" and class.bno="+tclass+" and tc.skxq="+sxq+" and class.bno=tc.bno and school.schno=class.schno and course.cno=tc.cno order by tcno desc");
                DataTable dt = mydatabase.GetDataTable(sql);
                dataGridView1.DataSource = dt.DefaultView;
            }
        }

    }
}