yginfo.cs
上传用户:toshiba_01
上传日期:2007-06-08
资源大小:306k
文件大小:8k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data ;
- using System.Data .SqlClient ;
- namespace RoomManage
- {
- /// <summary>
- /// yginfo 的摘要说明。
- /// </summary>
- public class yginfo : System.Windows.Forms.Form
- {
- private System.Windows.Forms.DataGrid dataGrid1;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.Button button3;
- private System.Windows.Forms.Button button4;
-
- DataConn dc=new DataConn ();
- protected SqlDataAdapter adapter;
- protected DataSet dataset;
- protected DataTable table;
- protected string selectString;
-
- public yginfo()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- BindData();
- //
- // 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.dataGrid1 = new System.Windows.Forms.DataGrid();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.button1 = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.button2 = new System.Windows.Forms.Button();
- this.button3 = new System.Windows.Forms.Button();
- this.button4 = new System.Windows.Forms.Button();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.SuspendLayout();
- //
- // dataGrid1
- //
- this.dataGrid1.DataMember = "";
- this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid1.Location = new System.Drawing.Point(3, 17);
- this.dataGrid1.Name = "dataGrid1";
- this.dataGrid1.Size = new System.Drawing.Size(402, 196);
- this.dataGrid1.TabIndex = 0;
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.dataGrid1);
- this.groupBox1.Location = new System.Drawing.Point(0, 0);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(408, 216);
- this.groupBox1.TabIndex = 1;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "员工信息";
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(8, 16);
- this.button1.Name = "button1";
- this.button1.TabIndex = 2;
- this.button1.Text = "添加";
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.button1);
- this.groupBox2.Controls.Add(this.button2);
- this.groupBox2.Controls.Add(this.button3);
- this.groupBox2.Controls.Add(this.button4);
- this.groupBox2.Location = new System.Drawing.Point(0, 216);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(408, 48);
- this.groupBox2.TabIndex = 3;
- this.groupBox2.TabStop = false;
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(88, 16);
- this.button2.Name = "button2";
- this.button2.TabIndex = 2;
- this.button2.Text = "删除";
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // button3
- //
- this.button3.Location = new System.Drawing.Point(240, 16);
- this.button3.Name = "button3";
- this.button3.TabIndex = 2;
- this.button3.Text = "保存";
- this.button3.Click += new System.EventHandler(this.button3_Click);
- //
- // button4
- //
- this.button4.Location = new System.Drawing.Point(320, 16);
- this.button4.Name = "button4";
- this.button4.TabIndex = 2;
- this.button4.Text = "退出";
- this.button4.Click += new System.EventHandler(this.button4_Click);
- //
- // yginfo
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(408, 288);
- this.ControlBox = false;
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.MaximizeBox = false;
- this.MaximumSize = new System.Drawing.Size(416, 296);
- this.MinimizeBox = false;
- this.MinimumSize = new System.Drawing.Size(416, 296);
- this.Name = "yginfo";
- this.ShowInTaskbar = false;
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
- this.Text = "员工信息维护";
- this.TopMost = true;
- this.Closing += new System.ComponentModel.CancelEventHandler(this.yginfo_Closing);
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- private void BindData()
- {
- string connString=dc.connstr ;
- SqlConnection conn=new SqlConnection(connString);
- selectString="select * from tabyginfo order by 员工编号";
- adapter=new SqlDataAdapter(selectString,conn);
- SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
- adapter.DeleteCommand=builder.GetDeleteCommand();
- adapter.InsertCommand=builder.GetInsertCommand();
- adapter.UpdateCommand=builder.GetUpdateCommand();
- dataset=new DataSet();
- adapter.Fill(dataset,"tabyginfo");
- table=dataset.Tables["tabyginfo"];
-
- //创建惟一性约束
- UniqueConstraint constraint = new UniqueConstraint
- (new DataColumn[]{table.Columns[0]});
- table.Constraints.Add(constraint);
- this.dataGrid1.SetDataBinding(dataset,"tabyginfo");
- }
- public virtual void AddNewRow()
- {
- int num=table.Rows.Count+1;
- DataRow newRow=table.NewRow();
- while(true)
- {
- try
- {
- newRow["员工编号"]="YG"+num.ToString ("d5");
- table.Rows.Add(newRow);
- break;
- }
- catch
- {
- num++;
- continue;
- }
- }
- }
- private void button4_Click(object sender, System.EventArgs e)
- {
- if(MessageBox.Show("真的要退出吗?","注意",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
- this.Close ();
- }
- private void button3_Click(object sender, System.EventArgs e)
- {
- try
- {
- adapter.Update(dataset,"tabyginfo");
- MessageBox.Show("保存成功。","恭喜");
- }
- catch(Exception err)
- {
- MessageBox.Show(err.Message,"保存信息出错!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
- }
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- AddNewRow();
- }
- private void button2_Click(object sender, System.EventArgs e)
- {
- int num=table.Rows.Count-1;
- if(num>0)
- {
- ArrayList ar=new ArrayList();
- for(int i=num;i>=0;i--)
- {
- if(this.dataGrid1.IsSelected(i))
- {
- ar.Add(i);
- }
- }
- if(ar.Count>0)
- {
- if(MessageBox.Show("确实要删除选中的"+ar.Count.ToString()+"行吗? 注意:删除后就无法恢复了!","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
- {
- foreach(int i in ar)
- {
- table.Rows[i].Delete();
- }
- }
- }
- else
- {
- MessageBox.Show("请先用鼠标点击左边灰色部分选择要删除的行(可以按住Ctrl键或者Shift键同时用鼠标选中多行),然后再删除。","提示");
- }
- }
- }
- private void yginfo_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if(dataset.HasChanges())
- {
- if(MessageBox.Show("数据尚未保存,放弃所作的修改吗?","小心",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
- {
- this.Close();
- e.Cancel=false; //退出
- }
- else
- {
- e.Cancel=true; //放弃退出
- }
- }
- }
- }
- }