SellTable.cs
上传用户:hjieqiu
上传日期:2013-05-11
资源大小:16494k
文件大小:28k
源码类别:

企业管理

开发平台:

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. namespace 进销存管理系统
  8. {
  9. /// <summary>
  10. /// SellTable 的摘要说明。
  11. /// </summary>
  12. public class SellTable : System.Windows.Forms.Form
  13. {
  14. private LinkDataBase link = new LinkDataBase();
  15. private DataTable newTable;
  16. public static string [] inputDataGridArray = new string [] {null,null,null,null};
  17. public static string [] inputTextDataArray = new string [] {"",""}; //确定要赋的值为文本,故用空字符
  18. private DateTime gridMouseDownTime;//记录事件,确定鼠标是双击表格还是选中一个单元
  19. private System.Windows.Forms.GroupBox groupBox1;
  20. private System.Windows.Forms.Label label4;
  21. private System.Windows.Forms.Label label5;
  22. private System.Windows.Forms.Label label6;
  23. private System.Windows.Forms.Label label7;
  24. private System.Windows.Forms.DataGrid dgrd_SellTable;
  25. private System.Windows.Forms.ComboBox cmb_Oprater;
  26. private System.Windows.Forms.TextBox txt_SellDate;
  27. private System.Windows.Forms.TextBox txt_Maker;
  28. private System.Windows.Forms.TextBox txt_Company;
  29. private System.Windows.Forms.ToolBar toolBar1;
  30. private System.Windows.Forms.ToolBarButton tbarBtnSave;
  31. private System.Windows.Forms.ToolBarButton tbarBtnDelete;
  32. private System.Windows.Forms.ImageList imageList1;
  33. private System.Windows.Forms.GroupBox groupBox2;
  34. private System.Windows.Forms.TextBox txt_TotalCount;
  35. private System.Windows.Forms.Label label1;
  36. private System.Windows.Forms.Label label2;
  37. private System.Windows.Forms.TextBox txt_NoTax;
  38. private System.Windows.Forms.Label label3;
  39. private System.Windows.Forms.TextBox txt_Tax;
  40. private System.Windows.Forms.ToolBarButton tbarTrash;
  41. private System.Windows.Forms.ComboBox cmb_ClientID;
  42. private System.ComponentModel.IContainer components;
  43. public SellTable()
  44. {
  45. //
  46. // Windows 窗体设计器支持所必需的
  47. //
  48. InitializeComponent();
  49. //
  50. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  51. //
  52. this.selectDataBase();
  53. this.DataGridStateControl();
  54. this.cmb_ClientID.Items.Add("");
  55. }
  56. /// <summary>
  57. /// 清理所有正在使用的资源。
  58. /// </summary>
  59. protected override void Dispose( bool disposing )
  60. {
  61. if( disposing )
  62. {
  63. if(components != null)
  64. {
  65. components.Dispose();
  66. }
  67. }
  68. base.Dispose( disposing );
  69. }
  70. #region Windows 窗体设计器生成的代码
  71. /// <summary>
  72. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  73. /// 此方法的内容。
  74. /// </summary>
  75. private void InitializeComponent()
  76. {
  77. this.components = new System.ComponentModel.Container();
  78. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SellTable));
  79. this.dgrd_SellTable = new System.Windows.Forms.DataGrid();
  80. this.groupBox1 = new System.Windows.Forms.GroupBox();
  81. this.cmb_Oprater = new System.Windows.Forms.ComboBox();
  82. this.cmb_ClientID = new System.Windows.Forms.ComboBox();
  83. this.txt_SellDate = new System.Windows.Forms.TextBox();
  84. this.txt_Maker = new System.Windows.Forms.TextBox();
  85. this.label4 = new System.Windows.Forms.Label();
  86. this.txt_Company = new System.Windows.Forms.TextBox();
  87. this.label5 = new System.Windows.Forms.Label();
  88. this.label6 = new System.Windows.Forms.Label();
  89. this.label7 = new System.Windows.Forms.Label();
  90. this.toolBar1 = new System.Windows.Forms.ToolBar();
  91. this.tbarBtnSave = new System.Windows.Forms.ToolBarButton();
  92. this.tbarBtnDelete = new System.Windows.Forms.ToolBarButton();
  93. this.tbarTrash = new System.Windows.Forms.ToolBarButton();
  94. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  95. this.groupBox2 = new System.Windows.Forms.GroupBox();
  96. this.txt_TotalCount = new System.Windows.Forms.TextBox();
  97. this.label1 = new System.Windows.Forms.Label();
  98. this.label2 = new System.Windows.Forms.Label();
  99. this.txt_NoTax = new System.Windows.Forms.TextBox();
  100. this.label3 = new System.Windows.Forms.Label();
  101. this.txt_Tax = new System.Windows.Forms.TextBox();
  102. ((System.ComponentModel.ISupportInitialize)(this.dgrd_SellTable)).BeginInit();
  103. this.groupBox1.SuspendLayout();
  104. this.groupBox2.SuspendLayout();
  105. this.SuspendLayout();
  106. // 
  107. // dgrd_SellTable
  108. // 
  109. this.dgrd_SellTable.AllowSorting = false;
  110. this.dgrd_SellTable.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  111. | System.Windows.Forms.AnchorStyles.Left) 
  112. | System.Windows.Forms.AnchorStyles.Right)));
  113. this.dgrd_SellTable.CaptionVisible = false;
  114. this.dgrd_SellTable.DataMember = "";
  115. this.dgrd_SellTable.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  116. this.dgrd_SellTable.Location = new System.Drawing.Point(0, 104);
  117. this.dgrd_SellTable.Name = "dgrd_SellTable";
  118. this.dgrd_SellTable.Size = new System.Drawing.Size(720, 264);
  119. this.dgrd_SellTable.TabIndex = 5;
  120. this.dgrd_SellTable.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dgrd_SellTable_MouseDown);
  121. this.dgrd_SellTable.DoubleClick += new System.EventHandler(this.dgrd_SellTable_DoubleClick);
  122. this.dgrd_SellTable.CurrentCellChanged += new System.EventHandler(this.dgrd_SellTable_CurrentCellChanged);
  123. // 
  124. // groupBox1
  125. // 
  126. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  127. | System.Windows.Forms.AnchorStyles.Right)));
  128. this.groupBox1.Controls.Add(this.cmb_Oprater);
  129. this.groupBox1.Controls.Add(this.cmb_ClientID);
  130. this.groupBox1.Controls.Add(this.txt_SellDate);
  131. this.groupBox1.Controls.Add(this.txt_Maker);
  132. this.groupBox1.Controls.Add(this.label4);
  133. this.groupBox1.Controls.Add(this.txt_Company);
  134. this.groupBox1.Controls.Add(this.label5);
  135. this.groupBox1.Controls.Add(this.label6);
  136. this.groupBox1.Controls.Add(this.label7);
  137. this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  138. this.groupBox1.Location = new System.Drawing.Point(0, 22);
  139. this.groupBox1.Name = "groupBox1";
  140. this.groupBox1.Size = new System.Drawing.Size(720, 82);
  141. this.groupBox1.TabIndex = 0;
  142. this.groupBox1.TabStop = false;
  143. // 
  144. // cmb_Oprater
  145. // 
  146. this.cmb_Oprater.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  147. this.cmb_Oprater.Location = new System.Drawing.Point(525, 16);
  148. this.cmb_Oprater.Name = "cmb_Oprater";
  149. this.cmb_Oprater.Size = new System.Drawing.Size(120, 20);
  150. this.cmb_Oprater.Sorted = true;
  151. this.cmb_Oprater.TabIndex = 3;
  152. // 
  153. // cmb_ClientID
  154. // 
  155. this.cmb_ClientID.Location = new System.Drawing.Point(143, 51);
  156. this.cmb_ClientID.MaxDropDownItems = 1;
  157. this.cmb_ClientID.Name = "cmb_ClientID";
  158. this.cmb_ClientID.Size = new System.Drawing.Size(128, 20);
  159. this.cmb_ClientID.Sorted = true;
  160. this.cmb_ClientID.TabIndex = 4;
  161. this.cmb_ClientID.DropDown += new System.EventHandler(this.cmb_Client_DropDown);
  162. this.cmb_ClientID.KeyUp += new System.Windows.Forms.KeyEventHandler(this.cmb_Client_KeyUp);
  163. // 
  164. // txt_SellDate
  165. // 
  166. this.txt_SellDate.Location = new System.Drawing.Point(143, 16);
  167. this.txt_SellDate.Name = "txt_SellDate";
  168. this.txt_SellDate.Size = new System.Drawing.Size(128, 21);
  169. this.txt_SellDate.TabIndex = 1;
  170. this.txt_SellDate.Text = "";
  171. // 
  172. // txt_Maker
  173. // 
  174. this.txt_Maker.Location = new System.Drawing.Point(346, 16);
  175. this.txt_Maker.Name = "txt_Maker";
  176. this.txt_Maker.TabIndex = 2;
  177. this.txt_Maker.Text = "sys";
  178. // 
  179. // label4
  180. // 
  181. this.label4.Location = new System.Drawing.Point(75, 55);
  182. this.label4.Name = "label4";
  183. this.label4.Size = new System.Drawing.Size(62, 16);
  184. this.label4.TabIndex = 19;
  185. this.label4.Text = "客户编号:";
  186. // 
  187. // txt_Company
  188. // 
  189. this.txt_Company.Location = new System.Drawing.Point(293, 51);
  190. this.txt_Company.Name = "txt_Company";
  191. this.txt_Company.ReadOnly = true;
  192. this.txt_Company.Size = new System.Drawing.Size(352, 21);
  193. this.txt_Company.TabIndex = 9;
  194. this.txt_Company.TabStop = false;
  195. this.txt_Company.Text = "";
  196. // 
  197. // label5
  198. // 
  199. this.label5.Location = new System.Drawing.Point(293, 22);
  200. this.label5.Name = "label5";
  201. this.label5.Size = new System.Drawing.Size(48, 16);
  202. this.label5.TabIndex = 17;
  203. this.label5.Text = "制单人:";
  204. // 
  205. // label6
  206. // 
  207. this.label6.Location = new System.Drawing.Point(471, 22);
  208. this.label6.Name = "label6";
  209. this.label6.Size = new System.Drawing.Size(48, 16);
  210. this.label6.TabIndex = 18;
  211. this.label6.Text = "业务员:";
  212. // 
  213. // label7
  214. // 
  215. this.label7.Location = new System.Drawing.Point(75, 22);
  216. this.label7.Name = "label7";
  217. this.label7.Size = new System.Drawing.Size(62, 16);
  218. this.label7.TabIndex = 16;
  219. this.label7.Text = "售货日期:";
  220. // 
  221. // toolBar1
  222. // 
  223. this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  224. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  225. this.tbarBtnSave,
  226. this.tbarBtnDelete,
  227. this.tbarTrash});
  228. this.toolBar1.DropDownArrows = true;
  229. this.toolBar1.ImageList = this.imageList1;
  230. this.toolBar1.Location = new System.Drawing.Point(0, 0);
  231. this.toolBar1.Name = "toolBar1";
  232. this.toolBar1.ShowToolTips = true;
  233. this.toolBar1.Size = new System.Drawing.Size(720, 29);
  234. this.toolBar1.TabIndex = 8;
  235. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  236. // 
  237. // tbarBtnSave
  238. // 
  239. this.tbarBtnSave.ImageIndex = 0;
  240. this.tbarBtnSave.ToolTipText = "保存修改";
  241. // 
  242. // tbarBtnDelete
  243. // 
  244. this.tbarBtnDelete.ImageIndex = 1;
  245. this.tbarBtnDelete.ToolTipText = "删除数据";
  246. // 
  247. // tbarTrash
  248. // 
  249. this.tbarTrash.ImageIndex = 2;
  250. this.tbarTrash.ToolTipText = "存入历史";
  251. // 
  252. // imageList1
  253. // 
  254. this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
  255. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  256. this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
  257. // 
  258. // groupBox2
  259. // 
  260. this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
  261. | System.Windows.Forms.AnchorStyles.Right)));
  262. this.groupBox2.Controls.Add(this.txt_TotalCount);
  263. this.groupBox2.Controls.Add(this.label1);
  264. this.groupBox2.Controls.Add(this.label2);
  265. this.groupBox2.Controls.Add(this.txt_NoTax);
  266. this.groupBox2.Controls.Add(this.label3);
  267. this.groupBox2.Controls.Add(this.txt_Tax);
  268. this.groupBox2.Location = new System.Drawing.Point(0, 374);
  269. this.groupBox2.Name = "groupBox2";
  270. this.groupBox2.Size = new System.Drawing.Size(720, 46);
  271. this.groupBox2.TabIndex = 9;
  272. this.groupBox2.TabStop = false;
  273. // 
  274. // txt_TotalCount
  275. // 
  276. this.txt_TotalCount.Location = new System.Drawing.Point(123, 13);
  277. this.txt_TotalCount.Name = "txt_TotalCount";
  278. this.txt_TotalCount.TabIndex = 6;
  279. this.txt_TotalCount.Text = "0.00";
  280. this.txt_TotalCount.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
  281. // 
  282. // label1
  283. // 
  284. this.label1.Location = new System.Drawing.Point(59, 18);
  285. this.label1.Name = "label1";
  286. this.label1.Size = new System.Drawing.Size(64, 16);
  287. this.label1.TabIndex = 46;
  288. this.label1.Text = "合计金额:";
  289. // 
  290. // label2
  291. // 
  292. this.label2.Location = new System.Drawing.Point(283, 18);
  293. this.label2.Name = "label2";
  294. this.label2.Size = new System.Drawing.Size(72, 16);
  295. this.label2.TabIndex = 44;
  296. this.label2.Text = "不含税金额:";
  297. // 
  298. // txt_NoTax
  299. // 
  300. this.txt_NoTax.Location = new System.Drawing.Point(363, 13);
  301. this.txt_NoTax.Name = "txt_NoTax";
  302. this.txt_NoTax.TabIndex = 7;
  303. this.txt_NoTax.Text = "0.00";
  304. this.txt_NoTax.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
  305. // 
  306. // label3
  307. // 
  308. this.label3.Location = new System.Drawing.Point(515, 18);
  309. this.label3.Name = "label3";
  310. this.label3.Size = new System.Drawing.Size(40, 16);
  311. this.label3.TabIndex = 45;
  312. this.label3.Text = "税额:";
  313. // 
  314. // txt_Tax
  315. // 
  316. this.txt_Tax.Location = new System.Drawing.Point(562, 13);
  317. this.txt_Tax.Name = "txt_Tax";
  318. this.txt_Tax.TabIndex = 8;
  319. this.txt_Tax.Text = "0.00";
  320. this.txt_Tax.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
  321. // 
  322. // SellTable
  323. // 
  324. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  325. this.ClientSize = new System.Drawing.Size(720, 421);
  326. this.Controls.Add(this.groupBox2);
  327. this.Controls.Add(this.toolBar1);
  328. this.Controls.Add(this.groupBox1);
  329. this.Controls.Add(this.dgrd_SellTable);
  330. this.Name = "SellTable";
  331. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  332. this.Text = "【销售单】";
  333. this.Load += new System.EventHandler(this.SellTable_Load);
  334. ((System.ComponentModel.ISupportInitialize)(this.dgrd_SellTable)).EndInit();
  335. this.groupBox1.ResumeLayout(false);
  336. this.groupBox2.ResumeLayout(false);
  337. this.ResumeLayout(false);
  338. }
  339. #endregion
  340. //---------在表格中填入所选商品名称,税率等信息---------
  341. public void setWareData()
  342. {
  343. dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,0] = inputDataGridArray[0];
  344. dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,4] = inputDataGridArray[1];
  345. dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,5] = inputDataGridArray[2];
  346. dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,7] = 17;
  347. }
  348. //----------在表格中填入仓库信息------
  349. public void setStorageData()
  350. {
  351. dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,3] = inputDataGridArray[3];
  352. }
  353. //---------------设置客户编号和客户名称--------------
  354. private void setTextData()
  355. {
  356. this.cmb_ClientID.IntegralHeight = false;//使组合框不调整大小以显示其所有项
  357. this.cmb_ClientID.DroppedDown = false;//使组合框不显示其下拉部分
  358. this.cmb_ClientID.Items[0] = inputTextDataArray[0];
  359. this.cmb_ClientID.SelectedIndex = 0;
  360. this.txt_Company.Text = inputTextDataArray[1];
  361. this.cmb_ClientID.IntegralHeight = true;//恢复默认值
  362. }
  363. //------------向【业务员】下拉列表框中添加下拉选项------------
  364. private void selectDataBase()
  365. {
  366. this.txt_SellDate.Text = System.DateTime.Today.ToShortDateString();
  367. string tempStrSQL = "select distinct 姓名 from 用户清单 ";
  368. DataTable tempDataTable = link.SelectDataBase(tempStrSQL);
  369. for (int i=0;i<tempDataTable.Rows.Count;i++)
  370. this.cmb_Oprater.Items.Add(tempDataTable.Rows[i][0]);
  371. this.cmb_Oprater.SelectedIndex = 0;
  372. }
  373. //----------绑定数据并设置明细表格各列的属性----------
  374. private void DataGridStateControl()
  375. {
  376. newTable = new DataTable();
  377. newTable.Columns.Add("货号(双击)",typeof(string));
  378. newTable.Columns.Add("数量",typeof(decimal));
  379. newTable.Columns.Add("单价",typeof(decimal));
  380. newTable.Columns.Add("仓库(双击)",typeof(string));
  381. newTable.Columns.Add("品名",typeof(string));
  382. newTable.Columns.Add("单位",typeof(string));
  383. newTable.Columns.Add("金额",typeof(decimal),"数量*单价");
  384. newTable.Columns.Add("税率",typeof(decimal));
  385. newTable.Columns.Add("不含税额",typeof(decimal),"金额/1.17");
  386. newTable.Columns.Add("税额",typeof(decimal),"金额-不含税额");
  387. this.dgrd_SellTable.DataSource = newTable;
  388. newTable.Rows.Add(newTable.NewRow()); //向表中添加一行
  389. DataGridTableStyle ts = new DataGridTableStyle();
  390. DataGridTextBoxColumn aColumnTextColumn;
  391. ts.AllowSorting = false;
  392. ts.AlternatingBackColor = Color.LightGray;
  393. ts.MappingName = newTable.TableName;
  394. int numCols = newTable.Columns.Count;
  395. for (int i = 0;i< numCols;i++)
  396. {
  397. aColumnTextColumn = new DataGridTextBoxColumn();
  398. if( i!=0 && i!=1 && i!=2 && i!=3)//表中只允许编辑【数量】和【单价】两个字段
  399. {
  400. aColumnTextColumn.ReadOnly=true;
  401. }
  402. if ( i == 0 || i == 3 )//当鼠标单击第0列和第3列时,允许响应
  403. {
  404. aColumnTextColumn.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
  405. }
  406. if ( i == 4 )
  407. {
  408. aColumnTextColumn.Width = 160;
  409. }
  410. aColumnTextColumn.MappingName = newTable.Columns[i].ColumnName;
  411. aColumnTextColumn.HeaderText = newTable.Columns[i].ColumnName;
  412. aColumnTextColumn.NullText = "";
  413. aColumnTextColumn.Format = "N"; //设置为数字格式显示
  414. ts.GridColumnStyles.Add(aColumnTextColumn);
  415. }
  416. this.dgrd_SellTable.TableStyles.Add(ts);
  417. }
  418. //-----------处理保存修改,删除和存入历史等功能---------
  419. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  420. {
  421. if (e.Button.ToolTipText == "保存修改")
  422. {
  423. clickedSaveIcon();
  424. }
  425. else if(e.Button.ToolTipText == "删除数据")
  426. {
  427. try
  428. {
  429. if (newTable.Rows.Count > 0)
  430. {
  431. this.newTable.Rows.RemoveAt(this.dgrd_SellTable.CurrentCell.RowNumber);
  432. this.controlCalculate();
  433. }
  434. //加入空行保证newTable中不为空,否则删除所有数据行后保存数据和MouseUp函数将引发异常出错
  435. if (newTable.Rows.Count-1 <= 0)
  436. newTable.Rows.Add(newTable.NewRow());
  437. }
  438. catch
  439. {
  440. return;
  441. }
  442. }
  443. else if (e.Button.ToolTipText == "存入历史")
  444. {
  445. clickedTrashIcon();
  446. }
  447. }
  448. //----------创建窗体,共用户选择客户----------
  449. private void cmb_Client_DropDown(object sender, System.EventArgs e)
  450. {
  451. ClientDataManage newFrm = new ClientDataManage(false);
  452. newFrm.setDataGridReadOnly();
  453. newFrm.ShowDialog();
  454. setTextData();
  455. SendKeys.Send("{Tab}");//向活动应用程序发送Tab键,跳到下一控件
  456. }
  457. //--------将所选客户的相关数据读入窗体---------
  458. private void cmb_Client_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  459. {
  460. this.txt_Company.Text = "";
  461. string strSearchWord = this.cmb_ClientID.Text;
  462. string sendSQL = "select 客户编号,名称 from 客户清单 where 客户编号 = '" + strSearchWord + "'";
  463. DataTable tempDataTable = this.link.SelectDataBase(sendSQL);
  464. if (tempDataTable.Rows.Count>0)
  465. {
  466. inputTextDataArray[0] = tempDataTable.Rows[0][0].ToString().Trim();
  467. inputTextDataArray[1] = tempDataTable.Rows[0][1].ToString().Trim();
  468. this.setTextData();
  469. SendKeys.Send("{Tab}");//向活动应用程序发送Tab键,跳到下一控件
  470. }
  471. }
  472. private void dgrd_SellTable_DoubleClick(object sender, System.EventArgs e)
  473. {
  474. this.doubleClicked();
  475. }
  476. private void dgrd_SellTable_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  477. {
  478. //获取当前点击鼠标时的时间
  479. DataGrid myGrid   = (DataGrid)sender;
  480. System.Windows.Forms.DataGrid.HitTestInfo myHitTest;
  481. // Use the DataGrid control's HitTest method with the x and y properties.
  482. myHitTest = this.dgrd_SellTable.HitTest(e.X,e.Y);
  483. if ( myHitTest.Type == System.Windows.Forms.DataGrid.HitTestType.Cell )
  484. gridMouseDownTime = DateTime.Now;
  485. }
  486. private void dgrd_SellTable_CurrentCellChanged(object sender, System.EventArgs e)
  487. {
  488. this.selectWareMessage();
  489. this.controlCalculate();
  490. }
  491. //------------处理鼠标在dataGrid上的单击事件-----------
  492. private void TextBoxMouseDownHandler(object sender, MouseEventArgs e )
  493. {
  494. //第一个判断条件:在单元格的textbox中的双击(即单击DataCell,使它获得焦点后,然后再响应该Cell中的双击事件)
  495. //第二个判断条件:DataGrid 的 DoubleClick(直接双击Cell就响应双击事件,无须先单击Cell,使它获得焦点,然后再响应双击事件)
  496. //判断时间间隔是否小于控制面板中所定义的双击间隔时间
  497. if( e.Button == MouseButtons.Left && e.Clicks == 2 || DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
  498. {
  499. this.doubleClicked();
  500. }
  501. }
  502. //------------在表格中双击,创建窗体以供选择货物或选择仓库-------------
  503. private void doubleClicked()
  504. {
  505. if (this.dgrd_SellTable.CurrentCell.ColumnNumber == 0 )//若鼠标单击的是第一列,则为选择货物
  506. {
  507. this.dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,3] = inputDataGridArray[3] = "";
  508. WareDataManage newFrm = new WareDataManage(false);
  509. newFrm.setDataGridReadOnly(true);
  510. newFrm.ShowDialog();
  511. setWareData();
  512. SendKeys.Send("{Tab}");//向活动应用程序发送Tab键,跳到下一控件(此处为下一格)
  513. }
  514. if (this.dgrd_SellTable.CurrentCell.ColumnNumber == 3)//选择仓库
  515. {
  516. string sendStrSQL = "select 仓库,库存数量 from 库存库 where 货号 = '" + dgrd_SellTable[dgrd_SellTable.CurrentCell.RowNumber,0].ToString().Trim() + "' order by 仓库 ASC";
  517. SelectStorage newFrm = new SelectStorage(sendStrSQL);
  518. newFrm.ShowDialog();
  519. setStorageData();
  520. //赋值完毕后跳向下一行
  521. this.dgrd_SellTable.CurrentCell = new DataGridCell(this.dgrd_SellTable.CurrentCell.ColumnNumber+1,0);
  522. }
  523. }
  524. //--------------向数据库提交数据--------------
  525. private void clickedSaveIcon()
  526. {
  527. try
  528. {
  529. //注意:必须先删除进货单明细表中的数据,然后才能再删除进货单中的数据
  530. string sendSQL = "delete 销售单明细";
  531. this.link.UpdateDataBase(sendSQL); //删除进货单明细中的数据
  532. sendSQL = "delete 销售单";
  533. this.link.UpdateDataBase(sendSQL); //删除进货单中的数据
  534. //向销售单中写入数据
  535. int intMaxSellNumberID= 0;
  536. sendSQL = "select 编号 from 销售单历史";
  537. //从销售单明细中读取"编号"列,取它的最大值,以便为新添加的数据编号
  538. DataTable intNumberTable = this.link.SelectDataBase(sendSQL);
  539. for (int i=0;i<intNumberTable.Rows.Count;i++)
  540. {
  541. if (intMaxSellNumberID < System.Int32.Parse(intNumberTable.Rows[i][0].ToString()))
  542. {
  543. intMaxSellNumberID = System.Int32.Parse(intNumberTable.Rows[i][0].ToString());
  544. }
  545. }
  546. sendSQL = "select 编号 from 销售单";
  547. //从销售单明细中读取"编号"列,取它的最大值,以便为新添加的数据编号
  548. intNumberTable = this.link.SelectDataBase(sendSQL);
  549. for (int i=0;i<intNumberTable.Rows.Count;i++)
  550. {
  551. if (intMaxSellNumberID < System.Int32.Parse(intNumberTable.Rows[i][0].ToString()))
  552. {
  553. intMaxSellNumberID = System.Int32.Parse(intNumberTable.Rows[i][0].ToString());
  554. }
  555. }
  556. intMaxSellNumberID++;
  557. string strClientID = this.cmb_ClientID.Items[0].ToString().Trim();
  558. string strSellDate = this.txt_SellDate.Text.Trim();
  559. string strOperator = this.cmb_Oprater.SelectedItem.ToString().Trim();
  560. string strMaker = this.txt_Maker.Text.Trim();
  561. decimal decTotalCount = Decimal.Parse(this.txt_TotalCount.Text.Trim());
  562. decimal decNoTax = Decimal.Parse(this.txt_NoTax.Text.Trim());
  563. decimal decTax =Decimal.Parse(this.txt_Tax.Text.Trim());
  564. string sendValues = "('" + intMaxSellNumberID +"','"+ strClientID +"','"+ strSellDate +"','"+ strOperator +"','"+
  565. strMaker +"','"+ decTotalCount +"','"+ decNoTax +"','"+ decTax + "')";
  566. sendSQL = "insert 销售单 (编号,客户编号,销售日期,业务员,制单人,税价合计,不含税价,税额) values " + sendValues;
  567. this.link.UpdateDataBase(sendSQL); 
  568. //向销售单明细中写入数据
  569. int intMaxNumberID = 0; 
  570. sendSQL = "select 编号 from 销售单明细历史";
  571. //从销售单明细中读取"编号"列,取它的最大值,以便为新添加的数据编号
  572. intNumberTable = this.link.SelectDataBase(sendSQL);
  573. for (int i=0;i<intNumberTable.Rows.Count;i++)
  574. {
  575. if (intMaxNumberID < System.Int32.Parse(intNumberTable.Rows[i][0].ToString()))
  576. {
  577. intMaxNumberID = System.Int32.Parse(intNumberTable.Rows[i][0].ToString());
  578. }
  579. }
  580. sendSQL = "select 编号 from 销售单明细";
  581. //从销售单明细中读取"编号"列,取它的最大值,以便为新添加的数据编号
  582. intNumberTable = this.link.SelectDataBase(sendSQL);
  583. for (int i=0;i<intNumberTable.Rows.Count;i++)
  584. {
  585. if (intMaxNumberID < System.Int32.Parse(intNumberTable.Rows[i][0].ToString()))
  586. {
  587. intMaxNumberID = System.Int32.Parse(intNumberTable.Rows[i][0].ToString());
  588. }
  589. }
  590. for (int row=0;row<this.newTable.Rows.Count;row++)
  591. {
  592. //首先判断表中的下一行数据是否为空,如果是则返回,以免因往数据库中写入空行而出错
  593. if(this.newTable.Rows[row][0].ToString() == "")
  594. {
  595. sendSQL = "delete from 销售单 where 编号 = '" + intMaxSellNumberID + "'";
  596. this.link.UpdateDataBase(sendSQL); //首先删除这次在销售单中加入的数据
  597. break;
  598. }
  599. else
  600. {
  601. intMaxNumberID++;
  602. string strWareID = this.dgrd_SellTable[row,0].ToString().Trim();
  603. decimal strWareCount = Decimal.Parse(this.dgrd_SellTable[row,1].ToString().Trim());
  604. decimal strWarePrice = Decimal.Parse(this.dgrd_SellTable[row,2].ToString().Trim());
  605. string strStorageID = this.dgrd_SellTable[row,3].ToString().Trim();
  606. sendValues = "('" + intMaxNumberID +"','"+intMaxSellNumberID +"','"+ strWareID +"','"+ strWareCount +"','"+ strWarePrice +"','"+
  607. decTotalCount +"','"+ 17 +"','"+ decNoTax +"','"+ decTax +"','"+ strStorageID + "')";
  608. sendSQL = "insert 销售单明细 (编号,销售单号,货号,销售数量,销售价,税价合计,税率,不含税价,税额,仓库) values " + sendValues;
  609. this.link.UpdateDataBase(sendSQL);
  610. }
  611. }
  612. MessageBox.Show("数据保存成功!","信息");
  613. }
  614. catch
  615. {
  616. MessageBox.Show("数据保存失败,请确认所有信息输入完整且正确!","提示");
  617. return;
  618. }
  619. }
  620. //---------将销售单数据存入历史记录---------
  621. private void clickedTrashIcon()
  622. {
  623. try
  624. {
  625. if (MessageBox.Show("确实要保存为历史记录吗?","询问",MessageBoxButtons.YesNo) == DialogResult.Yes)
  626. {
  627. this.link.UpdateDataBase("exec sf_销售单"); //调用存储过程
  628. //清除供货商信息
  629. this.cmb_ClientID.Items[0] = "";
  630. this.txt_Company.Text = "";
  631. //清除底部金额及税款信息
  632. this.txt_TotalCount.Text = "0.00";
  633. this.txt_NoTax.Text = "0.00";
  634. this.txt_Tax.Text = "0.00";
  635. //清除当前DataGrid中的所有数据
  636. newTable.Clear();
  637. //加入空行保证newTable中不为空,否则删除所有数据行后保存数据和MouseUp函数将引发异常出错
  638. newTable.Rows.Add(newTable.NewRow());
  639. MessageBox.Show("数据成功存入历史表!","信息");
  640. }
  641. }
  642. catch
  643. {
  644. MessageBox.Show("数据存入历史表时出错,请检查数据库!","提示");
  645. }
  646. }
  647. //------------将所选择的商品信息读入表格中------------
  648. private void selectWareMessage()
  649. {
  650. int intCurrentRowNumber = this.dgrd_SellTable.CurrentCell.RowNumber;
  651. //判断何时需要检索数据库的商品清单,读取商品信息(以免操作数据库次数过多,影响程序性能)
  652. if (this.dgrd_SellTable[intCurrentRowNumber,0].ToString() != "")
  653. {
  654. string strSearchWord = this.dgrd_SellTable[this.dgrd_SellTable.CurrentCell.RowNumber,0].ToString().Trim();
  655. string sendSQL = "select 货号,品名,单位 from 商品清单 where 货号 = '" + strSearchWord + "'";
  656. DataTable tempDataTable = this.link.SelectDataBase(sendSQL);
  657. if (tempDataTable.Rows.Count>0)
  658. {
  659. inputDataGridArray[0] = tempDataTable.Rows[0][0].ToString().Trim();
  660. inputDataGridArray[1] = tempDataTable.Rows[0][1].ToString().Trim();
  661. inputDataGridArray[2] = tempDataTable.Rows[0][2].ToString().Trim();
  662. this.setWareData();
  663. }
  664. //如果从数据库中没有检索出任何数据
  665. else
  666. {
  667. //inputDataGridArray[0]赋值为当前输入的值,方便用户再次修改输入
  668. inputDataGridArray[0] = this.dgrd_SellTable[intCurrentRowNumber,0].ToString().Trim(); 
  669. inputDataGridArray[1] = "";
  670. inputDataGridArray[2] = "";
  671. this.setWareData();
  672. }
  673. }
  674. }
  675. //------计算总价,税额等---------
  676. private void controlCalculate()
  677. {
  678. int intCurrentRowNumber = this.dgrd_SellTable.CurrentCell.RowNumber;
  679. string strWareCount = this.dgrd_SellTable[intCurrentRowNumber,1].ToString();
  680. string strWarePrice = this.dgrd_SellTable[intCurrentRowNumber,2].ToString();
  681. string strTotalCounter = this.dgrd_SellTable[intCurrentRowNumber,6].ToString();
  682. //当销售价和销售数量不为空,并且总金额为空时将当前光标移到下一行的开始处,以便DataTable依据表达式自动计算DataColumn的值
  683. if (strWareCount != "" && strWarePrice != "" && strTotalCounter=="")
  684. {
  685. this.dgrd_SellTable.CurrentCell = new DataGridCell(intCurrentRowNumber+1,0);
  686. }
  687. //为底部三个文本框计算总金额
  688. decimal intTotalCount = 0;
  689. decimal intNoTax = 0;
  690. decimal intTax = 0;
  691. //首先判断列是否为空
  692. object obj = newTable.Compute("SUM (金额)","");
  693. if (obj.GetType().ToString() != "System.DBNull")
  694. {
  695. intTotalCount = (decimal)this.newTable.Compute("SUM (金额)","");
  696. intNoTax = (decimal)this.newTable.Compute("SUM (不含税额)","");
  697. intTax = (decimal)this.newTable.Compute("SUM (税额)","");
  698. }
  699. this.txt_TotalCount.Text = intTotalCount.ToString("N");
  700. this.txt_NoTax.Text = intNoTax.ToString("N");
  701. this.txt_Tax.Text = intTax.ToString("N");
  702. }
  703. private void SellTable_Load(object sender, System.EventArgs e)
  704. {
  705. }
  706. }
  707. }