⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 curriculumschedule.cs

📁 由C开发的教务管理系统源码中的排课子系统
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;

namespace 教务管理系统
{
	/// <summary>
	/// CurriculumSchedule 的摘要说明。
	/// </summary>
	public class CurriculumSchedule : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private DataTable scheduleTable;//课表
		private DataTable curriculumTable;//保存学生的课程信息
		
		
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txt1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txt2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txt3;
		private System.Windows.Forms.Label label4;
		private System.ComponentModel.Container components = null;

		//重写构造函数,以判断是从选课窗体中调用课表还是直接显示课表
		public CurriculumSchedule(string viewID)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			if(viewID!="")
			{
				this.curriculumTable=new DataTable();
				this.DataGridStateControl();
			
				this.LoadStduentInfo(viewID);//读入学生信息和选课信息
				this.LoadCurriculumInfo(viewID);
				this.curriculmToSchedule();
			
				this.AutoSizeGrid();
				txt1.Text=viewID;//显示学号
				groupBox1.Enabled=false;//不允许改变显示的信息
			}
			else
			{
				this.curriculumTable=new DataTable();
				this.DataGridStateControl();
				this.AutoSizeGrid();
			}			
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txt1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txt2 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txt3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.CaptionVisible = false;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 72);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.RowHeaderWidth = 0;
			this.dataGrid1.Size = new System.Drawing.Size(648, 392);
			this.dataGrid1.TabIndex = 0;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txt1);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.txt2);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.txt3);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(644, 53);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "学生信息";
			// 
			// txt1
			// 
			this.txt1.Location = new System.Drawing.Point(88, 24);
			this.txt1.Name = "txt1";
			this.txt1.TabIndex = 1;
			this.txt1.Text = "";
			this.txt1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt1_KeyPress);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(40, 28);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "学号";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// txt2
			// 
			this.txt2.Location = new System.Drawing.Point(272, 24);
			this.txt2.Name = "txt2";
			this.txt2.ReadOnly = true;
			this.txt2.TabIndex = 1;
			this.txt2.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(224, 28);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "姓名";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// txt3
			// 
			this.txt3.Location = new System.Drawing.Point(480, 24);
			this.txt3.Name = "txt3";
			this.txt3.ReadOnly = true;
			this.txt3.TabIndex = 1;
			this.txt3.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(432, 28);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 0;
			this.label4.Text = "班级";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// CurriculumSchedule
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(664, 469);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.dataGrid1);
			this.Name = "CurriculumSchedule";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "【课程表】";
			
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		

		
		//----------设置明细表格各列的属性----------
		private void DataGridStateControl()
		{
			scheduleTable = new DataTable();
			scheduleTable.Columns.Add("星期一",typeof(string));
			scheduleTable.Columns.Add("星期二",typeof(string));		
			scheduleTable.Columns.Add("星期三",typeof(string));
			scheduleTable.Columns.Add("星期四",typeof(string));		
			scheduleTable.Columns.Add("星期五",typeof(string));

			
			this.dataGrid1.DataSource = scheduleTable;
			for(int i=0;i<7;i++)//向表中添加7行,代表一天有7节课
			{
				scheduleTable.Rows.Add(scheduleTable.NewRow());				
			}

			DataGridTableStyle ts = new DataGridTableStyle();
			DataGridTextBoxColumn aColumnTextColumn;			
			ts.AllowSorting = false;
			ts.AlternatingBackColor = Color.LightGray;
			ts.MappingName = scheduleTable.TableName;
			
			for (int i = 0;i< 5;i++)
			{
				aColumnTextColumn = new DataGridTextBoxColumn();
				aColumnTextColumn.ReadOnly=true;
				aColumnTextColumn.Width=120;
				aColumnTextColumn.TextBox.Height=50;

								
				aColumnTextColumn.MappingName = scheduleTable.Columns[i].ColumnName;
				aColumnTextColumn.HeaderText = scheduleTable.Columns[i].ColumnName;
				aColumnTextColumn.NullText = "";
				aColumnTextColumn.Format = "N"; //设置为数字格式显示
				ts.GridColumnStyles.Add(aColumnTextColumn);
			}
			dataGrid1.TableStyles.Add(ts);
		}

		//-----------读取学生的课表信息----------
		private void LoadCurriculumInfo(string id)
		{			this.curriculumTable.Clear();//清除该学生的所选课程信息			string strConn="workstation id=localhost;Integrated Security=SSPI;database=eisbook;";			SqlConnection cn=new SqlConnection(strConn);			cn.Open();
			string sql="select b.课程名称,b.教师,c.上课时间天,c.上课时间节,c.上课地点 "
				+"from 选课表 a,课程信息 b,课程表 c "
				+"where (a.课序号=c.课序号)and('200400000'+b.课程编号=a.课序号)"
				+"and(a.学号='"+id+"')";
			SqlDataAdapter da=new SqlDataAdapter(sql,cn);
			da.Fill(this.curriculumTable);//该学生的所选课程信息
		}

        //---------读取学生信息-----------
		private void LoadStduentInfo(string id)
		{						txt2.Clear();//清除学生信息			txt3.Clear();			string strConn="workstation id=localhost;Integrated Security=SSPI;database=eisbook;";			SqlConnection cn=new SqlConnection(strConn);			cn.Open();
			SqlCommand cmd=cn.CreateCommand();
			cmd.CommandText="select a.姓名,b.班级名称,a.学籍编号 from 学生信息 a,班级信息 b "
				+"where(a.班级编号=b.班级编号)and(学号='"+id+"')";
			SqlDataReader dr=cmd.ExecuteReader();
			dr.Read();//读入数据
			if(!dr.HasRows)//判断学号是否输入正确
			{
				MessageBox.Show("无此学生,请重新输入学号","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
				dr.Close();
				return;
			}
			
			if(dr.GetValue(2).ToString().Trim()!="1")//判断是否为在籍学生
			{
				MessageBox.Show("该学生并非在籍学生,无课表","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);                
				return;
			}
			txt2.Text=dr.GetValue(0).ToString().Trim();//姓名
			txt3.Text=dr.GetValue(1).ToString().Trim();//班级名称
			dr.Close();
		}

		//---------将所选课程信息填充在课程表中----------
		private void curriculmToSchedule()
		{
			this.scheduleTable.Clear();
			for(int i=0;i<7;i++)//向表中添加7行,代表一天有7节课
			{
				scheduleTable.Rows.Add(scheduleTable.NewRow());				
			}
			foreach(DataRow aRow in this.curriculumTable.Rows)
			{
				int day=Convert.ToInt32(aRow["上课时间天"]);
				int sec=Convert.ToInt32(aRow["上课时间节"]);
				this.scheduleTable.Rows[sec-1][day-1]=aRow["课程名称"].ToString().Trim()+"\n"
					+aRow["上课地点"].ToString().Trim()+"\n"
					+aRow["教师"].ToString().Trim();
			}

		}
		



		//----------自动设置dataGrid中的行宽度--------
		public void AutoSizeGrid() 
 
		{ 			 
			int numRows = ((DataTable)dataGrid1.DataSource).Rows.Count; //该dataGrid所连接的表的宽度 
			Graphics g = Graphics.FromHwnd(dataGrid1.Handle);  
			StringFormat sf = new StringFormat(StringFormat.GenericTypographic); 
			SizeF size; 
 
			//由于DataGrid没有提供对DataGridRows[]的直接调用,因此我们使用一个反射(relection)来实现它
			//这个方法实际上取得dataGrid中行的集合,通过get_DataGridRows方法返回Rows的集合
			//并转化为一个System.Array对象 
			MethodInfo mi = dataGrid1.GetType().GetMethod("get_DataGridRows", 
 
				BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Instance 
 
				| BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 
 
			System.Array dgra = (System.Array)mi.Invoke(dataGrid1,null); 
 
			// 将其转换为ArrayList
			ArrayList DataGridRows = new ArrayList();  
			foreach (object dgrr in dgra)  
			{  
				if (dgrr.ToString().EndsWith("DataGridRelationshipRow")==true) 
					DataGridRows.Add(dgrr);  
			} 
 
			//枚举dataGrid中的所有行 
			for (int i = 0; i < numRows; ++i) 
 			{
				size = g.MeasureString(dataGrid1[i,1].ToString(),dataGrid1.Font,400,sf); 
 				int h = Convert.ToInt32(40);//直接将每行的行高设置为40
				//用下面这行代码可以根据dataGrid中字体自动调整行高
				//int h = Convert.ToInt32(size.Height); 
				// 考虑到表格表头的高度,可以适当加高 
				h = h + 8;  
				
				// 将定好的行高设置回dataGrid中 
				PropertyInfo pi = DataGridRows[i].GetType().GetProperty("Height");  
				pi.SetValue(DataGridRows[i],h,null);  
			}  
			g.Dispose();  
		}

		//----------输入学号后回车,显示课表信息和学生信息-----------
		private void txt1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(txt1.Text.Trim()!=""&&e.KeyChar==13)
			{
				this.LoadStduentInfo(txt1.Text.Trim());//读入学生信息
				this.LoadCurriculumInfo(txt1.Text.Trim());//读入选课表信息
				this.curriculmToSchedule();//将选课内容显示在课表中
				this.AutoSizeGrid();
			}
		}  		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -