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

namespace WindowsApplication1
{
    public partial class Form14 : Form
    {
        public Form14()
        {
            InitializeComponent();
        }
        Buessiness teacher = new Buessiness();
        Mydatabase DB = new Mydatabase();
        /// <summary>
        ///  dataGridView1的数据绑定事件编码
        /// </summary>
        public void bind(int i)
        {
            string sql = "";
            if (i == 1)
            {
                sql = "select tcno,sktime from tc where tno=2 and cno in (select distinct cno from sc where isposted=1) order by tcno asc";

            }
            if (i == 2)
            {
                string skxq = comboBox1.SelectedValue.ToString();
                sql = "select tcno,sktime from tc where tno=2 and cno in (select distinct cno from sc where isposted=1) and skxq=" + skxq + " order by tcno desc";

            }
            if (i == 3)
            {
                string skbj = comboBox2.SelectedValue.ToString();
                sql = "select tcno,sktime from tc where tno=2 and cno in (select distinct cno from sc where isposted=1) and bno=" + skbj + " order by tcno desc";

            }
            DataTable dt = DB.GetDataTable(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.AllowUserToAddRows = false; 
        }
        /// <summary>
        /// 初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form14_Load(object sender, EventArgs e)
        {
            //首先，将教师的信息显示出来。假设我们进入系统教师的ID编号是2号，来自teacher表
            int user_id = 2;
            int dno, xb;//变量表示为单位编号和系部编号
            string zc, degree, dname, tname;
            //变量表示：职称、学位信息、单位名称和教师姓名
            teacher.Getteacherinfo(user_id, out dno, out xb, out zc, out degree, out dname, out tname);
            label6.Text = Convert.ToString(user_id).Trim();
            label7.Text = tname.Trim();
            label8.Text = degree.Trim();
            label9.Text = dname.Trim();
            label10.Text = zc.Trim();
            //其次，添加前五个学期的信息到comboBox1
            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";
            DataSet ds1 = DB.GetDataSet(sql2);
            comboBox1.DataSource = ds1.Tables[0];
            comboBox1.DisplayMember = "skxqname";
            comboBox1.ValueMember = "skxq";
            //然后，添加按照班级查询信息
            string sql = "select bno,bname from class where bno in(select bno from tc where tno=2)";
            DataSet ds2 = DB.GetDataSet(sql);
            comboBox2.DataSource = ds2.Tables[0];
            comboBox2.DisplayMember = "bname";
            comboBox2.ValueMember = "bno";
            //最后，显示电子网格中的数据信息
            bind(1);
        }
        
 
        private void dataGridView1_RowPrePaint_1(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            int k = dataGridView1.Rows.Count;
            if (e.RowIndex != -1)
            {
                DataGridViewRow DGrow = dataGridView1.Rows[e.RowIndex];
                int i = Convert.ToInt32(DGrow.Cells["tcno"].Value);
                //此处需要获取课程表的课程性质名称
                int cno, tno, sktime, bno, zt, schno, stunum, yy, wy, ztbk, stunumbk, yybk, wybk, ztqk, stunumqk, yyqk, wyqk;
                string skxq, tname, cname, bname, skxqn, schname;
                teacher.GetCourselist(i, out cno, out tno, out skxq, out sktime, out bno, out tname, out cname, out bname, out skxqn, out zt, out schno, out schname, out stunum, out yy, out wy, out ztbk, out stunumbk, out yybk, out wybk, out ztqk, out stunumqk, out yyqk, out wyqk);
                DGrow.Cells["skxq"].Value = skxqn;
                DGrow.Cells["banji"].Value = bname;
                DGrow.Cells["school"].Value = schname;
                DGrow.Cells["coursename"].Value = cname;
                DGrow.Cells["sktime"].Value = sktime.ToString();
                DGrow.Cells["stunum"].Value = stunum.ToString();
                DGrow.Cells["yy"].Value = yy.ToString();
                DGrow.Cells["wy"].Value = wy.ToString();
            }
        }
        /// <summary>
        /// 按照学期信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            bind(2);
        }
        /// <summary>
        /// 按照课程查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            bind(3);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bind(1);
        }


    }
}