yginfo.cs
上传用户:toshiba_01
上传日期:2007-06-08
资源大小:306k
文件大小:8k
源码类别:

酒店行业

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data ;
  7. using System.Data .SqlClient ;
  8. namespace RoomManage
  9. {
  10. /// <summary>
  11. /// yginfo 的摘要说明。
  12. /// </summary>
  13. public class yginfo : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.DataGrid dataGrid1;
  16. /// <summary>
  17. /// 必需的设计器变量。
  18. /// </summary>
  19. private System.ComponentModel.Container components = null;
  20. private System.Windows.Forms.GroupBox groupBox1;
  21. private System.Windows.Forms.Button button1;
  22. private System.Windows.Forms.GroupBox groupBox2;
  23. private System.Windows.Forms.Button button2;
  24. private System.Windows.Forms.Button button3;
  25. private System.Windows.Forms.Button button4;
  26. DataConn dc=new DataConn ();
  27. protected SqlDataAdapter adapter;
  28. protected DataSet dataset;
  29. protected DataTable table;
  30. protected string selectString;
  31. public yginfo()
  32. {
  33. //
  34. // Windows 窗体设计器支持所必需的
  35. //
  36. InitializeComponent();
  37. BindData();
  38. //
  39. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40. //
  41. }
  42. /// <summary>
  43. /// 清理所有正在使用的资源。
  44. /// </summary>
  45. protected override void Dispose( bool disposing )
  46. {
  47. if( disposing )
  48. {
  49. if(components != null)
  50. {
  51. components.Dispose();
  52. }
  53. }
  54. base.Dispose( disposing );
  55. }
  56. #region Windows 窗体设计器生成的代码
  57. /// <summary>
  58. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  59. /// 此方法的内容。
  60. /// </summary>
  61. private void InitializeComponent()
  62. {
  63. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  64. this.groupBox1 = new System.Windows.Forms.GroupBox();
  65. this.button1 = new System.Windows.Forms.Button();
  66. this.groupBox2 = new System.Windows.Forms.GroupBox();
  67. this.button2 = new System.Windows.Forms.Button();
  68. this.button3 = new System.Windows.Forms.Button();
  69. this.button4 = new System.Windows.Forms.Button();
  70. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  71. this.groupBox1.SuspendLayout();
  72. this.groupBox2.SuspendLayout();
  73. this.SuspendLayout();
  74. // 
  75. // dataGrid1
  76. // 
  77. this.dataGrid1.DataMember = "";
  78. this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
  79. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  80. this.dataGrid1.Location = new System.Drawing.Point(3, 17);
  81. this.dataGrid1.Name = "dataGrid1";
  82. this.dataGrid1.Size = new System.Drawing.Size(402, 196);
  83. this.dataGrid1.TabIndex = 0;
  84. // 
  85. // groupBox1
  86. // 
  87. this.groupBox1.Controls.Add(this.dataGrid1);
  88. this.groupBox1.Location = new System.Drawing.Point(0, 0);
  89. this.groupBox1.Name = "groupBox1";
  90. this.groupBox1.Size = new System.Drawing.Size(408, 216);
  91. this.groupBox1.TabIndex = 1;
  92. this.groupBox1.TabStop = false;
  93. this.groupBox1.Text = "员工信息";
  94. // 
  95. // button1
  96. // 
  97. this.button1.Location = new System.Drawing.Point(8, 16);
  98. this.button1.Name = "button1";
  99. this.button1.TabIndex = 2;
  100. this.button1.Text = "添加";
  101. this.button1.Click += new System.EventHandler(this.button1_Click);
  102. // 
  103. // groupBox2
  104. // 
  105. this.groupBox2.Controls.Add(this.button1);
  106. this.groupBox2.Controls.Add(this.button2);
  107. this.groupBox2.Controls.Add(this.button3);
  108. this.groupBox2.Controls.Add(this.button4);
  109. this.groupBox2.Location = new System.Drawing.Point(0, 216);
  110. this.groupBox2.Name = "groupBox2";
  111. this.groupBox2.Size = new System.Drawing.Size(408, 48);
  112. this.groupBox2.TabIndex = 3;
  113. this.groupBox2.TabStop = false;
  114. // 
  115. // button2
  116. // 
  117. this.button2.Location = new System.Drawing.Point(88, 16);
  118. this.button2.Name = "button2";
  119. this.button2.TabIndex = 2;
  120. this.button2.Text = "删除";
  121. this.button2.Click += new System.EventHandler(this.button2_Click);
  122. // 
  123. // button3
  124. // 
  125. this.button3.Location = new System.Drawing.Point(240, 16);
  126. this.button3.Name = "button3";
  127. this.button3.TabIndex = 2;
  128. this.button3.Text = "保存";
  129. this.button3.Click += new System.EventHandler(this.button3_Click);
  130. // 
  131. // button4
  132. // 
  133. this.button4.Location = new System.Drawing.Point(320, 16);
  134. this.button4.Name = "button4";
  135. this.button4.TabIndex = 2;
  136. this.button4.Text = "退出";
  137. this.button4.Click += new System.EventHandler(this.button4_Click);
  138. // 
  139. // yginfo
  140. // 
  141. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  142. this.ClientSize = new System.Drawing.Size(408, 288);
  143. this.ControlBox = false;
  144. this.Controls.Add(this.groupBox2);
  145. this.Controls.Add(this.groupBox1);
  146. this.MaximizeBox = false;
  147. this.MaximumSize = new System.Drawing.Size(416, 296);
  148. this.MinimizeBox = false;
  149. this.MinimumSize = new System.Drawing.Size(416, 296);
  150. this.Name = "yginfo";
  151. this.ShowInTaskbar = false;
  152. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  153. this.Text = "员工信息维护";
  154. this.TopMost = true;
  155. this.Closing += new System.ComponentModel.CancelEventHandler(this.yginfo_Closing);
  156. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  157. this.groupBox1.ResumeLayout(false);
  158. this.groupBox2.ResumeLayout(false);
  159. this.ResumeLayout(false);
  160. }
  161. #endregion
  162. private void BindData()
  163. {
  164. string connString=dc.connstr ;
  165. SqlConnection conn=new SqlConnection(connString);
  166. selectString="select * from tabyginfo order by 员工编号";
  167. adapter=new SqlDataAdapter(selectString,conn);
  168. SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
  169. adapter.DeleteCommand=builder.GetDeleteCommand();
  170. adapter.InsertCommand=builder.GetInsertCommand();
  171. adapter.UpdateCommand=builder.GetUpdateCommand();
  172. dataset=new DataSet();
  173. adapter.Fill(dataset,"tabyginfo");
  174. table=dataset.Tables["tabyginfo"];
  175. //创建惟一性约束
  176. UniqueConstraint constraint = new UniqueConstraint
  177. (new DataColumn[]{table.Columns[0]});
  178. table.Constraints.Add(constraint);
  179. this.dataGrid1.SetDataBinding(dataset,"tabyginfo");
  180. }
  181. public virtual void AddNewRow()
  182. {
  183. int num=table.Rows.Count+1;
  184. DataRow newRow=table.NewRow();
  185. while(true)
  186. {
  187. try
  188. {
  189. newRow["员工编号"]="YG"+num.ToString ("d5");
  190. table.Rows.Add(newRow);
  191. break;
  192. }
  193. catch
  194. {
  195. num++;
  196. continue;
  197. }
  198. }
  199. }
  200. private void button4_Click(object sender, System.EventArgs e)
  201. {
  202. if(MessageBox.Show("真的要退出吗?","注意",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
  203. this.Close ();
  204. }
  205. private void button3_Click(object sender, System.EventArgs e)
  206. {
  207. try
  208. {
  209. adapter.Update(dataset,"tabyginfo");
  210. MessageBox.Show("保存成功。","恭喜");
  211. }
  212. catch(Exception err)
  213. {
  214. MessageBox.Show(err.Message,"保存信息出错!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  215. }
  216. }
  217. private void button1_Click(object sender, System.EventArgs e)
  218. {
  219. AddNewRow();
  220. }
  221. private void button2_Click(object sender, System.EventArgs e)
  222. {
  223. int num=table.Rows.Count-1;
  224. if(num>0)
  225. {
  226. ArrayList ar=new ArrayList();
  227. for(int i=num;i>=0;i--)
  228. {
  229. if(this.dataGrid1.IsSelected(i))
  230. {
  231. ar.Add(i);
  232. }
  233. }
  234. if(ar.Count>0)
  235. {
  236. if(MessageBox.Show("确实要删除选中的"+ar.Count.ToString()+"行吗? 注意:删除后就无法恢复了!","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
  237. {
  238. foreach(int i in ar)
  239. {
  240. table.Rows[i].Delete();
  241. }
  242. }
  243. }
  244. else
  245. {
  246. MessageBox.Show("请先用鼠标点击左边灰色部分选择要删除的行(可以按住Ctrl键或者Shift键同时用鼠标选中多行),然后再删除。","提示");
  247. }
  248. }
  249. }
  250. private void yginfo_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  251. {
  252. if(dataset.HasChanges())
  253. {
  254. if(MessageBox.Show("数据尚未保存,放弃所作的修改吗?","小心",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
  255. {
  256. this.Close();
  257. e.Cancel=false;  //退出
  258. }
  259. else
  260. {
  261. e.Cancel=true;  //放弃退出
  262. }
  263. }
  264. }
  265. }
  266. }