TrialBalance.cs
上传用户:hjieqiu
上传日期:2013-05-11
资源大小:16494k
文件大小: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 财务管理系统
  9. {
  10. /// <summary>
  11. /// TrialBalance 的摘要说明。
  12. /// </summary>
  13. public class TrialBalance : System.Windows.Forms.Form
  14. {
  15. private string sqlStr;
  16. private System.Windows.Forms.DataGrid dataGrid1;
  17. private System.Windows.Forms.GroupBox groupBox1;
  18. private System.Windows.Forms.DataGrid dataGrid2;
  19. /// <summary>
  20. /// 必需的设计器变量。
  21. /// </summary>
  22. private System.ComponentModel.Container components = null;
  23. public TrialBalance(bool isNew)//修改构造函数,以便在新建帐簿和试算平衡时分别使用
  24. {
  25. //
  26. // Windows 窗体设计器支持所必需的
  27. //
  28. InitializeComponent();
  29. //
  30. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  31. //
  32. if(isNew==true)
  33. {
  34. sqlStr="select 科目代码, 科目名称,(case when 余额方向='借方' then 期初余额 else 0 end - 累计借方)"
  35. +" as 期初借方,(case when 余额方向='贷方' then 期初余额 else 0 end - 累计贷方) as 期初贷方,"
  36. +"累计借方 as 本期发生借方,累计贷方 as 本期发生贷方,  case when 余额方向='借方' then 期初余额 "
  37. +"else 0 end as 期末借方,case when 余额方向='贷方' then 期初余额 else 0 end as 期末贷方 "
  38. +"from 帐簿初始化表 where 累计借方<>0 or 累计贷方<>0 or 期初余额<>0";
  39. }
  40. else
  41. {
  42. sqlStr="select a.科目代码, b.科目名称,(case when a.余额方向='借方' then 期初余额 else 0 end )"
  43. +" as 期初借方,(case when a.余额方向='贷方' then 期初余额 else 0 end) as 期初贷方,"
  44. +" 本期借方合计 as 本期发生借方, 本期贷方合计 as 本期发生贷方,"
  45. +"case when a.余额方向='借方' then 余额 else 0 end as 期末借方,"
  46. +"case when a.余额方向='贷方' then 余额 else 0 end as 期末贷方 from 本期汇总账簿 as a,"
  47. +"科目表 as b where a.科目代码 = b.科目代码 and (本期借方合计<> 0"
  48. +" or 本期贷方合计<>0 or 期初余额<>0 or 余额<>0)";
  49. }
  50. }
  51. /// <summary>
  52. /// 清理所有正在使用的资源。
  53. /// </summary>
  54. protected override void Dispose( bool disposing )
  55. {
  56. if( disposing )
  57. {
  58. if(components != null)
  59. {
  60. components.Dispose();
  61. }
  62. }
  63. base.Dispose( disposing );
  64. }
  65. #region Windows 窗体设计器生成的代码
  66. /// <summary>
  67. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  68. /// 此方法的内容。
  69. /// </summary>
  70. private void InitializeComponent()
  71. {
  72. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  73. this.groupBox1 = new System.Windows.Forms.GroupBox();
  74. this.dataGrid2 = new System.Windows.Forms.DataGrid();
  75. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  76. this.groupBox1.SuspendLayout();
  77. ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
  78. this.SuspendLayout();
  79. // 
  80. // dataGrid1
  81. // 
  82. this.dataGrid1.CaptionVisible = false;
  83. this.dataGrid1.DataMember = "";
  84. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  85. this.dataGrid1.Location = new System.Drawing.Point(0, 0);
  86. this.dataGrid1.Name = "dataGrid1";
  87. this.dataGrid1.ReadOnly = true;
  88. this.dataGrid1.Size = new System.Drawing.Size(640, 384);
  89. this.dataGrid1.TabIndex = 0;
  90. // 
  91. // groupBox1
  92. // 
  93. this.groupBox1.BackColor = System.Drawing.SystemColors.Control;
  94. this.groupBox1.Controls.Add(this.dataGrid2);
  95. this.groupBox1.Location = new System.Drawing.Point(0, 384);
  96. this.groupBox1.Name = "groupBox1";
  97. this.groupBox1.Size = new System.Drawing.Size(640, 88);
  98. this.groupBox1.TabIndex = 1;
  99. this.groupBox1.TabStop = false;
  100. this.groupBox1.Text = "试算平衡结果";
  101. // 
  102. // dataGrid2
  103. // 
  104. this.dataGrid2.CaptionVisible = false;
  105. this.dataGrid2.DataMember = "";
  106. this.dataGrid2.ForeColor = System.Drawing.Color.Red;
  107. this.dataGrid2.HeaderBackColor = System.Drawing.SystemColors.Desktop;
  108. this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  109. this.dataGrid2.Location = new System.Drawing.Point(8, 24);
  110. this.dataGrid2.Name = "dataGrid2";
  111. this.dataGrid2.ReadOnly = true;
  112. this.dataGrid2.SelectionForeColor = System.Drawing.SystemColors.Info;
  113. this.dataGrid2.Size = new System.Drawing.Size(624, 56);
  114. this.dataGrid2.TabIndex = 2;
  115. // 
  116. // TrialBalance
  117. // 
  118. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  119. this.ClientSize = new System.Drawing.Size(640, 477);
  120. this.Controls.Add(this.groupBox1);
  121. this.Controls.Add(this.dataGrid1);
  122. this.Name = "TrialBalance";
  123. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  124. this.Text = "【试算平衡表】";
  125. this.Load += new System.EventHandler(this.TrialBalance_Load);
  126. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  127. this.groupBox1.ResumeLayout(false);
  128. ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
  129. this.ResumeLayout(false);
  130. }
  131. #endregion
  132. //-------------创建窗体时显示数据-----------
  133. private void TrialBalance_Load(object sender, System.EventArgs e)
  134. {
  135. string strConn = "workstation id=localhost;Integrated Security=SSPI;database=caiwubook";
  136. SqlConnection cn=new SqlConnection(strConn);
  137. cn.Open();
  138. SqlDataAdapter da=new SqlDataAdapter(sqlStr,cn);
  139. DataTable newTable=new DataTable();
  140. da.Fill(newTable);
  141. dataGrid1.DataSource=newTable;
  142. this.sumTable(newTable);
  143. }
  144. //----------------统计数据,判断是否平衡--------------------
  145. private void sumTable(DataTable tbl)
  146. {
  147. decimal iniDebit=0;//期初借方
  148. decimal iniLoan=0;//期初贷方
  149. decimal thisDebit=0;//本期发生借方
  150. decimal thisLoan=0;//本期发生贷方
  151. decimal finDebit=0;//期末借方
  152. decimal finLoan=0;//期末贷方
  153. string isBalance="平衡";
  154. if(tbl.Rows.Count>0)
  155. {
  156. foreach(DataRow aRow in tbl.Rows)
  157. {
  158. iniDebit+=Convert.ToDecimal(aRow["期初借方"]);
  159. iniLoan+=Convert.ToDecimal(aRow["期初贷方"]);
  160. thisDebit+=Convert.ToDecimal(aRow["本期发生借方"]);
  161. thisLoan+=Convert.ToDecimal(aRow["本期发生贷方"]);
  162. finDebit+=Convert.ToDecimal(aRow["期末借方"]);
  163. finLoan+=Convert.ToDecimal(aRow["期末贷方"]);
  164. }
  165. if(iniDebit!=iniLoan||thisDebit!=thisLoan||finDebit!=finLoan)
  166. isBalance="不平衡";
  167. }
  168. DataTable sumTable = new DataTable();
  169. sumTable.Columns.Add("是否平衡",typeof(string));
  170. sumTable.Columns.Add("期初借方",typeof(decimal));
  171. sumTable.Columns.Add("期初贷方",typeof(decimal));
  172. sumTable.Columns.Add("本期发生借方",typeof(decimal));
  173. sumTable.Columns.Add("本期发生贷方",typeof(decimal));
  174. sumTable.Columns.Add("期末借方",typeof(decimal));
  175. sumTable.Columns.Add("期末贷方",typeof(decimal));
  176. this.dataGrid2.DataSource = sumTable;
  177. sumTable.Rows.Add(sumTable.NewRow()); //向表中添加一行
  178. //设置表格格式
  179. DataGridTableStyle ts = new DataGridTableStyle();
  180. DataGridTextBoxColumn aColumnTextColumn;
  181. ts.AllowSorting = false;
  182. ts.MappingName = sumTable.TableName;
  183. int numCols = sumTable.Columns.Count;
  184. for (int i = 0;i< numCols;i++)
  185. {
  186. aColumnTextColumn = new DataGridTextBoxColumn();
  187. aColumnTextColumn.MappingName = sumTable.Columns[i].ColumnName;
  188. aColumnTextColumn.HeaderText = sumTable.Columns[i].ColumnName;
  189. aColumnTextColumn.NullText = "";
  190. aColumnTextColumn.Format = "N"; //设置为数字格式显示
  191. ts.GridColumnStyles.Add(aColumnTextColumn);
  192. }
  193. dataGrid2.TableStyles.Add(ts);
  194. //将结果显示在表格中
  195. dataGrid2[0,0]=isBalance;
  196. dataGrid2[0,1]=iniDebit.ToString();
  197. dataGrid2[0,2]=iniLoan.ToString();
  198. dataGrid2[0,3]=thisDebit.ToString();
  199. dataGrid2[0,4]=thisLoan.ToString();
  200. dataGrid2[0,5]=finDebit.ToString();
  201. dataGrid2[0,6]=finLoan.ToString();
  202. if(isBalance=="不平衡")//如果试算结果不平衡,则用红色来提醒用户
  203. {
  204. groupBox1.Text="试算结果不平衡!";
  205. groupBox1.BackColor=Color.Red;
  206. }
  207. else
  208. {
  209. groupBox1.Text="试算结果平衡";
  210. }
  211. }
  212. }
  213. }