?? sortform.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>
/// Sort 的摘要說明。
/// </summary>
public class Sort : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid SortdataGrid;
private SqlConnection SortConnection = null;
private SqlCommand SortSelectCommand = null;
private SqlCommand SortDeleteCommand = null;
private SqlDataAdapter SortDataAdapter = null;
private DataSet ds = null;
private string sqlString = null;
private int Row = -1;
private System.Windows.Forms.Button addBtn;
private System.Windows.Forms.Button updateBtn;
private System.Windows.Forms.Button deleteBtn;
private System.Windows.Forms.Button selectBtn;
private System.Windows.Forms.TextBox FLMtextBox;
private System.Windows.Forms.Label FLMlabel;
private System.Windows.Forms.Button returnBtn;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Sort()
{
//
// 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.addBtn = new System.Windows.Forms.Button();
this.updateBtn = new System.Windows.Forms.Button();
this.deleteBtn = new System.Windows.Forms.Button();
this.selectBtn = new System.Windows.Forms.Button();
this.FLMtextBox = new System.Windows.Forms.TextBox();
this.FLMlabel = new System.Windows.Forms.Label();
this.SortdataGrid = new System.Windows.Forms.DataGrid();
this.returnBtn = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.SortdataGrid)).BeginInit();
this.SuspendLayout();
//
// addBtn
//
this.addBtn.Location = new System.Drawing.Point(32, 32);
this.addBtn.Name = "addBtn";
this.addBtn.Size = new System.Drawing.Size(120, 32);
this.addBtn.TabIndex = 0;
this.addBtn.Text = "添加";
this.addBtn.Click += new System.EventHandler(this.addBtn_Click);
//
// updateBtn
//
this.updateBtn.Location = new System.Drawing.Point(200, 32);
this.updateBtn.Name = "updateBtn";
this.updateBtn.Size = new System.Drawing.Size(120, 32);
this.updateBtn.TabIndex = 1;
this.updateBtn.Text = "修改";
this.updateBtn.Click += new System.EventHandler(this.updateBtn_Click);
//
// deleteBtn
//
this.deleteBtn.Location = new System.Drawing.Point(368, 32);
this.deleteBtn.Name = "deleteBtn";
this.deleteBtn.Size = new System.Drawing.Size(120, 32);
this.deleteBtn.TabIndex = 2;
this.deleteBtn.Text = "刪除";
this.deleteBtn.Click += new System.EventHandler(this.deleteBtn_Click);
//
// selectBtn
//
this.selectBtn.Location = new System.Drawing.Point(536, 80);
this.selectBtn.Name = "selectBtn";
this.selectBtn.Size = new System.Drawing.Size(96, 32);
this.selectBtn.TabIndex = 3;
this.selectBtn.Text = "查詢";
this.selectBtn.Click += new System.EventHandler(this.selectBtn_Click);
//
// FLMtextBox
//
this.FLMtextBox.Location = new System.Drawing.Point(312, 80);
this.FLMtextBox.Name = "FLMtextBox";
this.FLMtextBox.Size = new System.Drawing.Size(200, 30);
this.FLMtextBox.TabIndex = 4;
this.FLMtextBox.Text = "";
//
// FLMlabel
//
this.FLMlabel.Location = new System.Drawing.Point(24, 80);
this.FLMlabel.Name = "FLMlabel";
this.FLMlabel.Size = new System.Drawing.Size(280, 32);
this.FLMlabel.TabIndex = 5;
this.FLMlabel.Text = "請輸入所要查詢的分類名:";
//
// SortdataGrid
//
this.SortdataGrid.DataMember = "";
this.SortdataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.SortdataGrid.Location = new System.Drawing.Point(16, 128);
this.SortdataGrid.Name = "SortdataGrid";
this.SortdataGrid.ReadOnly = true;
this.SortdataGrid.Size = new System.Drawing.Size(664, 328);
this.SortdataGrid.TabIndex = 6;
this.SortdataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SortdataGrid_MouseDown);
this.SortdataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.SortdataGrid_MouseUp);
//
// returnBtn
//
this.returnBtn.Location = new System.Drawing.Point(544, 32);
this.returnBtn.Name = "returnBtn";
this.returnBtn.Size = new System.Drawing.Size(120, 32);
this.returnBtn.TabIndex = 7;
this.returnBtn.Text = "返回";
this.returnBtn.Click += new System.EventHandler(this.returnBtn_Click);
//
// Sort
//
this.AutoScaleBaseSize = new System.Drawing.Size(11, 23);
this.ClientSize = new System.Drawing.Size(696, 470);
this.Controls.Add(this.returnBtn);
this.Controls.Add(this.SortdataGrid);
this.Controls.Add(this.FLMlabel);
this.Controls.Add(this.FLMtextBox);
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 = "Sort";
this.Text = "分類信息維護";
this.Load += new System.EventHandler(this.Sort_Load);
((System.ComponentModel.ISupportInitialize)(this.SortdataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Sort_Load(object sender, System.EventArgs e)
{
//string connString = "user id=sa;data source=\".\";initial catalog=KTMIS";
//SortConnection = new SqlConnection(connString);
SortSelectCommand = new SqlCommand("",SortConnection);
SortDeleteCommand = new SqlCommand("DeleteSort",SortConnection);
sqlString = "select SortNa as 分類名稱,SortPY as 分類拼音縮寫 from Sort";
ds = new DataSet();
try
{
SortDataAdapter = new SqlDataAdapter(sqlString,SortConnection);
SortDataAdapter.Fill(ds,"Sort");
}
catch(Exception ex)
{
MessageBox.Show("載入錯誤!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
SortdataGrid.SetDataBinding(ds,"Sort");
}
private void addBtn_Click(object sender, System.EventArgs e)
{
addSort aSort = new addSort();
aSort.Connection = this.SortConnection;
aSort.ShowDialog();
Initial();
}
private void updateBtn_Click(object sender, System.EventArgs e)
{
string name = null;
string py = null;
DataTable dt = ds.Tables["Sort"];
for(int i = 0;i<dt.Rows.Count;i++)
{
if(SortdataGrid.IsSelected(i))
{
name = dt.Rows[i][0].ToString();
py = dt.Rows[i][1].ToString();
break;
}
}
if(name==null)
{
MessageBox.Show("請選擇在表框中所要修改的分類的行!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
string sortCo = null;
string selectString = "select SortCo from Sort where SortNa=\'"+name+"\' and SortPY=\'"+py+"\'";
SortSelectCommand.CommandText = selectString;
try
{
SortConnection.Open();
SqlDataReader dr = SortSelectCommand.ExecuteReader();
while(dr.Read())
{
sortCo = dr.GetValue(0).ToString();
break;
}
}
catch(Exception ex)
{
MessageBox.Show(" 讀取數據失敗!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
finally
{
SortConnection.Close();
}
if(sortCo==null)
{
MessageBox.Show("操作失敗,請重試!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
addSort aSort = new addSort();
aSort.SetBtnText("修改");
aSort.SetSortCoText(sortCo.ToString());
aSort.SetSNText(name);
aSort.SetSNPYText(py);
aSort.SetFormTital("修改分類");
aSort.ShowDialog();
Initial();
}
private void Initial()
{
ds.Clear();
SortDataAdapter.SelectCommand.CommandText = sqlString;
try
{
SortConnection.Open();
SortDataAdapter.Fill(ds,"Sort");
SortdataGrid.SetDataBinding(ds,"Sort");
}
catch(Exception e)
{
MessageBox.Show("初始化錯誤!原因是"+e.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
SortConnection.Close();
}
}
private void SortdataGrid_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 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;
string py = null;
DataTable dt = ds.Tables["Sort"];
for(int i = 0;i<dt.Rows.Count;i++)
{
if(SortdataGrid.IsSelected(i))
{
name = dt.Rows[i][0].ToString();
py = dt.Rows[i][1].ToString();
break;
}
}
if(name==null)
{
MessageBox.Show("請選擇在表框中所要修改的分類的行!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
string sortCo = null;
string selectString = "select SortCo from Sort where SortNa=\'"+name+"\' and SortPY=\'"+py+"\'";
SortSelectCommand.CommandText = selectString;
try
{
SortConnection.Open();
SqlDataReader dr = SortSelectCommand.ExecuteReader();
while(dr.Read())
{
sortCo = dr.GetValue(0).ToString();
break;
}
}
catch(Exception ex)
{
MessageBox.Show(" 讀取數據失敗!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{
SortConnection.Close();
}
if(sortCo==null)
{
MessageBox.Show("操作失敗,請重試!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
SortDeleteCommand.CommandType = CommandType.StoredProcedure;
SortDeleteCommand.Parameters.Clear();
SortDeleteCommand.Parameters.Add("@SortCo",SqlDbType.Int);
SortDeleteCommand.Parameters["@SortCo"].Value = Convert.ToInt16(sortCo.Trim());
try
{
SortConnection.Open();
SortDeleteCommand.ExecuteNonQuery();
MessageBox.Show("刪除成功!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show("刪除失敗,原因是:"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return;
}
finally
{
SortConnection.Close();
}
Initial();
}
private void SortdataGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(Row!=-1)
{
SortdataGrid.Select(Row);
}
}
private void selectBtn_Click(object sender, System.EventArgs e)
{
string sqlStr = "select SortNa as 分類名稱,SortPY as 分類拼音縮寫 from Sort where SortNa like \'%" +FLMtextBox.Text.Trim()+"%\'";
ds.Clear();
try
{
SortDataAdapter.SelectCommand.CommandText = sqlStr;
SortDataAdapter.Fill(ds,"Sort");
}
catch(Exception ex)
{
MessageBox.Show("載入錯誤!原因是"+ex.ToString(),"系統提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
SortdataGrid.SetDataBinding(ds,"Sort");
}
private void returnBtn_Click(object sender, System.EventArgs e)
{
this.Close();
}
public System.Data.SqlClient.SqlConnection Connection
{
set
{
this.SortConnection = value;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -