?? curriculumschedule.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 + -