?? 副本 collegeform.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace KTMIS
{
/// <summary>
/// colege 的摘要說明。
/// </summary>
public class college : System.Windows.Forms.Form
{
private System.Windows.Forms.Button returnBtn;
private System.Windows.Forms.DataGrid collegedataGrid;
private System.Windows.Forms.Label collegelabel;
private System.Windows.Forms.TextBox collegetextBox;
private System.Windows.Forms.Button selectBtn;
private System.Windows.Forms.Button deleteBtn;
private System.Windows.Forms.Button updateBtn;
private System.Windows.Forms.Button addBtn;
private SqlConnection collegeConnection = null;
private SqlCommand collegeSelectCommand = null;
private SqlCommand collegeDeleteCommand = null;
private SqlDataAdapter collegeDataAdapter = null;
private DataSet ds = null;
private string sqlString = null;
private int Row = -1;
private System.Windows.Forms.Label colegelabel;
private System.Windows.Forms.TextBox colegetextBox;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public college()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
/// <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.returnBtn = new System.Windows.Forms.Button();
this.collegedataGrid = new System.Windows.Forms.DataGrid();
this.colegelabel = new System.Windows.Forms.Label();
this.colegetextBox = new System.Windows.Forms.TextBox();
this.selectBtn = new System.Windows.Forms.Button();
this.deleteBtn = new System.Windows.Forms.Button();
this.updateBtn = new System.Windows.Forms.Button();
this.addBtn = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.collegedataGrid)).BeginInit();
this.SuspendLayout();
//
// returnBtn
//
this.returnBtn.Location = new System.Drawing.Point(544, 8);
this.returnBtn.Name = "returnBtn";
this.returnBtn.Size = new System.Drawing.Size(120, 32);
this.returnBtn.TabIndex = 23;
this.returnBtn.Text = "返回";
this.returnBtn.Click += new System.EventHandler(this.returnBtn_Click);
//
// collegedataGrid
//
this.collegedataGrid.DataMember = "";
this.collegedataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.collegedataGrid.Location = new System.Drawing.Point(16, 104);
this.collegedataGrid.Name = "collegedataGrid";
this.collegedataGrid.ReadOnly = true;
this.collegedataGrid.Size = new System.Drawing.Size(664, 328);
this.collegedataGrid.TabIndex = 22;
this.collegedataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.colegedataGrid_MouseDown);
this.collegedataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.colegedataGrid_MouseUp);
//
// colegelabel
//
this.colegelabel.Location = new System.Drawing.Point(24, 56);
this.colegelabel.Name = "colegelabel";
this.colegelabel.Size = new System.Drawing.Size(256, 24);
this.colegelabel.TabIndex = 24;
this.colegelabel.Text = "請輸入所要查詢的學院名稱:";
//
// colegetextBox
//
this.colegetextBox.Location = new System.Drawing.Point(312, 56);
this.colegetextBox.Name = "colegetextBox";
this.colegetextBox.Size = new System.Drawing.Size(184, 30);
this.colegetextBox.TabIndex = 25;
this.colegetextBox.Text = "";
//
// selectBtn
//
this.selectBtn.Location = new System.Drawing.Point(536, 56);
this.selectBtn.Name = "selectBtn";
this.selectBtn.Size = new System.Drawing.Size(96, 32);
this.selectBtn.TabIndex = 19;
this.selectBtn.Text = "查詢";
this.selectBtn.Click += new System.EventHandler(this.selectBtn_Click);
//
// deleteBtn
//
this.deleteBtn.Location = new System.Drawing.Point(368, 8);
this.deleteBtn.Name = "deleteBtn";
this.deleteBtn.Size = new System.Drawing.Size(120, 32);
this.deleteBtn.TabIndex = 18;
this.deleteBtn.Text = "刪除";
this.deleteBtn.Click += new System.EventHandler(this.deleteBtn_Click);
//
// updateBtn
//
this.updateBtn.Location = new System.Drawing.Point(200, 8);
this.updateBtn.Name = "updateBtn";
this.updateBtn.Size = new System.Drawing.Size(120, 32);
this.updateBtn.TabIndex = 17;
this.updateBtn.Text = "修改";
this.updateBtn.Click += new System.EventHandler(this.updateBtn_Click);
//
// addBtn
//
this.addBtn.Location = new System.Drawing.Point(32, 8);
this.addBtn.Name = "addBtn";
this.addBtn.Size = new System.Drawing.Size(120, 32);
this.addBtn.TabIndex = 16;
this.addBtn.Text = "添加";
this.addBtn.Click += new System.EventHandler(this.addBtn_Click);
//
// college
//
this.AutoScaleBaseSize = new System.Drawing.Size(11, 23);
this.ClientSize = new System.Drawing.Size(696, 454);
this.Controls.Add(this.returnBtn);
this.Controls.Add(this.collegedataGrid);
this.Controls.Add(this.colegelabel);
this.Controls.Add(this.colegetextBox);
this.Controls.Add(this.selectBtn);
this.Controls.Add(this.deleteBtn);
this.Controls.Add(this.updateBtn);
this.Controls.Add(this.addBtn);
this.Font = new System.Drawing.Font("宋體", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.Name = "college";
this.Text = "院系信息維護";
this.Load += new System.EventHandler(this.colege_Load);
((System.ComponentModel.ISupportInitialize)(this.collegedataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void colegedataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid myGrid = (DataGrid) sender;
DataGrid.HitTestInfo hti;
hti = myGrid.HitTest(e.X, e.Y);
if(hti.Type==DataGrid.HitTestType.Cell)
{
Row = hti.Row;
}
else
{
Row = -1;
}
}
private void colegedataGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Row!=-1)
{
collegedataGrid.Select(Row);
}
}
private void colege_Load(object sender, System.EventArgs e)
{
//string connString = "user id=sa;data source=\".\";initial catalog=KTMIS";
//collegeConnection = new SqlConnection(connString);
collegeSelectCommand = new SqlCommand("",collegeConnection);
collegeDeleteCommand = new SqlCommand("DeleteCollege",collegeConnection);
sqlString = "select CollegeNa as 院系名稱 from College";
ds = new DataSet();
try
{
collegeDataAdapter = new SqlDataAdapter(sqlString,collegeConnection);
collegeDataAdapter.Fill(ds,"college");
}
catch(Exception ex)
{
MessageBox.Show("載入錯誤!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
collegedataGrid.SetDataBinding(ds,"college");
}
private void Initial()
{
ds.Clear();
collegeDataAdapter.SelectCommand.CommandText = sqlString;
try
{
collegeConnection.Open();
collegeDataAdapter.Fill(ds,"college");
collegedataGrid.SetDataBinding(ds,"college");
}
catch(Exception e)
{
MessageBox.Show("初始化錯誤!原因是"+e.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
collegeConnection.Close();
}
}
private void addBtn_Click(object sender, System.EventArgs e)
{
AddCollege ac = new AddCollege();
ac.Connection = this.collegeConnection;
ac.ShowDialog();
Initial();
}
private void updateBtn_Click(object sender, System.EventArgs e)
{
string name = null;
DataTable dt = ds.Tables["college"];
for(int i = 0;i<dt.Rows.Count;i++)
{
if(collegedataGrid.IsSelected(i))
{
name = dt.Rows[i][0].ToString();
break;
}
}
if(name==null)
{
MessageBox.Show("請選擇在表框中所要修改的分類的行!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
string collegeCo = null;
string selectString = "select CollegeCo from college where CollegeNa=\'"+name+"\'";
collegeSelectCommand.CommandText = selectString;
try
{
collegeConnection.Open();
SqlDataReader dr = collegeSelectCommand.ExecuteReader();
while(dr.Read())
{
collegeCo = dr.GetValue(0).ToString();
break;
}
}
catch(Exception ex)
{
MessageBox.Show(" 讀取數據失敗!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
finally
{
collegeConnection.Close();
}
if(collegeCo==null)
{
MessageBox.Show("操作失敗,請重試!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
AddCollege ac = new AddCollege();
ac.Connection = this.collegeConnection;
ac.SetcollegeBtnText("修改");
ac.SetcollegeCoText(collegeCo.ToString());
ac.SetcollegeSNText(name);
ac.SetFormTital("修改院系");
ac.ShowDialog();
Initial();
}
private void deleteBtn_Click(object sender, System.EventArgs e)
{
string questionStr = "若刪除該院系信息,與其相關的課題信息亦將同時被刪除!\n\n是否繼續?";
DialogResult dialogResult = MessageBox.Show(questionStr, "系統提問", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dialogResult == DialogResult.No)
{
return;
}
string name = null;
DataTable dt = ds.Tables["college"];
for(int i = 0;i<dt.Rows.Count;i++)
{
if(collegedataGrid.IsSelected(i))
{
name = dt.Rows[i][0].ToString();
break;
}
}
if(name==null)
{
MessageBox.Show("請選擇在表框中所要修改的分類的行!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
string collegeCo = null;
string selectString = "select CollegeCo from College where CollegeNa=\'"+name+"\'";
collegeSelectCommand.CommandText = selectString;
try
{
collegeConnection.Open();
SqlDataReader dr = collegeSelectCommand.ExecuteReader();
while(dr.Read())
{
collegeCo = dr.GetValue(0).ToString();
break;
}
}
catch(Exception ex)
{
MessageBox.Show(" 讀取數據失敗!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
finally
{
collegeConnection.Close();
}
if(collegeCo==null)
{
MessageBox.Show("操作失敗,請重試!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
collegeDeleteCommand.CommandType = CommandType.StoredProcedure;
collegeDeleteCommand.Parameters.Clear();
collegeDeleteCommand.Parameters.Add("@CollegeCo",SqlDbType.Int);
collegeDeleteCommand.Parameters["@CollegeCo"].Value = Convert.ToInt16(collegeCo.Trim());
try
{
collegeConnection.Open();
collegeDeleteCommand.ExecuteNonQuery();
MessageBox.Show("刪除成功!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show("刪除失敗,原因是:"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return;
}
finally
{
collegeConnection.Close();
}
Initial();
}
private void returnBtn_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void selectBtn_Click(object sender, System.EventArgs e)
{
string sqlStr = "select CollegeNa as 院系名稱 from College where CollegeNa like \'%" +collegetextBox.Text.Trim()+"%\'";
ds.Clear();
try
{
collegeDataAdapter.SelectCommand.CommandText = sqlStr;
collegeDataAdapter.Fill(ds,"college");
}
catch(Exception ex)
{
MessageBox.Show("載入錯誤!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
collegedataGrid.SetDataBinding(ds,"college");
}
public System.Data.SqlClient.SqlConnection Connection
{
set
{
this.collegeConnection = value;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -