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