⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trialbalance.cs

📁 用vs2003和SQLServer2000开发的财务管理系统
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace 财务管理系统
{
	/// <summary>
	/// TrialBalance 的摘要说明。
	/// </summary>
	public class TrialBalance : System.Windows.Forms.Form
	{
		private string sqlStr;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.DataGrid dataGrid2;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public TrialBalance(bool isNew)//修改构造函数,以便在新建帐簿和试算平衡时分别使用
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			if(isNew==true)
			{
				sqlStr="select 科目代码, 科目名称,(case when 余额方向='借方' then 期初余额 else 0 end - 累计借方)"
					+" as 期初借方,(case when 余额方向='贷方' then 期初余额 else 0 end - 累计贷方) as 期初贷方,"
					+"累计借方 as 本期发生借方,累计贷方 as 本期发生贷方,  case when 余额方向='借方' then 期初余额 "
					+"else 0 end as 期末借方,case when 余额方向='贷方' then 期初余额 else 0 end as 期末贷方 "
					+"from 帐簿初始化表 where 累计借方<>0 or 累计贷方<>0 or 期初余额<>0";
			}
			else
			{
				sqlStr="select a.科目代码, b.科目名称,(case when a.余额方向='借方' then 期初余额 else 0 end )"
					+" as 期初借方,(case when a.余额方向='贷方' then 期初余额 else 0 end) as 期初贷方,"
					+" 本期借方合计 as 本期发生借方, 本期贷方合计 as 本期发生贷方,"
					+"case when a.余额方向='借方' then 余额 else 0 end as 期末借方,"
					+"case when a.余额方向='贷方' then 余额 else 0 end as 期末贷方 from 本期汇总账簿 as a,"
					+"科目表 as b where a.科目代码 = b.科目代码 and (本期借方合计<> 0"
					+" or 本期贷方合计<>0 or 期初余额<>0 or 余额<>0)";
			}
		}

		/// <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.dataGrid2 = new System.Windows.Forms.DataGrid();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.CaptionVisible = false;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.Size = new System.Drawing.Size(640, 384);
			this.dataGrid1.TabIndex = 0;
			// 
			// groupBox1
			// 
			this.groupBox1.BackColor = System.Drawing.SystemColors.Control;
			this.groupBox1.Controls.Add(this.dataGrid2);
			this.groupBox1.Location = new System.Drawing.Point(0, 384);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(640, 88);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "试算平衡结果";
			// 
			// dataGrid2
			// 
			this.dataGrid2.CaptionVisible = false;
			this.dataGrid2.DataMember = "";
			this.dataGrid2.ForeColor = System.Drawing.Color.Red;
			this.dataGrid2.HeaderBackColor = System.Drawing.SystemColors.Desktop;
			this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid2.Location = new System.Drawing.Point(8, 24);
			this.dataGrid2.Name = "dataGrid2";
			this.dataGrid2.ReadOnly = true;
			this.dataGrid2.SelectionForeColor = System.Drawing.SystemColors.Info;
			this.dataGrid2.Size = new System.Drawing.Size(624, 56);
			this.dataGrid2.TabIndex = 2;
			// 
			// TrialBalance
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(640, 477);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.dataGrid1);
			this.Name = "TrialBalance";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "【试算平衡表】";
			this.Load += new System.EventHandler(this.TrialBalance_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		//-------------创建窗体时显示数据-----------
		private void TrialBalance_Load(object sender, System.EventArgs e)
		{
			string strConn = "workstation id=localhost;Integrated Security=SSPI;database=caiwubook";
			SqlConnection cn=new SqlConnection(strConn);
			cn.Open();
			SqlDataAdapter da=new SqlDataAdapter(sqlStr,cn);
			DataTable newTable=new DataTable();
			
			da.Fill(newTable);
			dataGrid1.DataSource=newTable;

			this.sumTable(newTable);

		}
		//----------------统计数据,判断是否平衡--------------------
		private void sumTable(DataTable tbl)
		{
			decimal iniDebit=0;//期初借方
			decimal iniLoan=0;//期初贷方
			decimal thisDebit=0;//本期发生借方
			decimal thisLoan=0;//本期发生贷方
			decimal finDebit=0;//期末借方
			decimal finLoan=0;//期末贷方
			string isBalance="平衡";
			if(tbl.Rows.Count>0)
			{
				foreach(DataRow aRow in tbl.Rows)
				{
					iniDebit+=Convert.ToDecimal(aRow["期初借方"]);
					iniLoan+=Convert.ToDecimal(aRow["期初贷方"]);
					thisDebit+=Convert.ToDecimal(aRow["本期发生借方"]);
					thisLoan+=Convert.ToDecimal(aRow["本期发生贷方"]);
					finDebit+=Convert.ToDecimal(aRow["期末借方"]);
					finLoan+=Convert.ToDecimal(aRow["期末贷方"]);
				}
				if(iniDebit!=iniLoan||thisDebit!=thisLoan||finDebit!=finLoan)
					isBalance="不平衡";
			}

			DataTable sumTable = new DataTable();
			sumTable.Columns.Add("是否平衡",typeof(string));
			
			sumTable.Columns.Add("期初借方",typeof(decimal));
			sumTable.Columns.Add("期初贷方",typeof(decimal));
			sumTable.Columns.Add("本期发生借方",typeof(decimal));
			sumTable.Columns.Add("本期发生贷方",typeof(decimal));
			sumTable.Columns.Add("期末借方",typeof(decimal));
			sumTable.Columns.Add("期末贷方",typeof(decimal));
			
			
			this.dataGrid2.DataSource = sumTable;
			sumTable.Rows.Add(sumTable.NewRow()); //向表中添加一行

			//设置表格格式
			DataGridTableStyle ts = new DataGridTableStyle();
			DataGridTextBoxColumn aColumnTextColumn;
			ts.AllowSorting = false;
			
			ts.MappingName = sumTable.TableName;
			int numCols = sumTable.Columns.Count;
			
			for (int i = 0;i< numCols;i++)
			{
				aColumnTextColumn = new DataGridTextBoxColumn();
				aColumnTextColumn.MappingName = sumTable.Columns[i].ColumnName;
				aColumnTextColumn.HeaderText = sumTable.Columns[i].ColumnName;
				aColumnTextColumn.NullText = "";
				aColumnTextColumn.Format = "N"; //设置为数字格式显示
				
				ts.GridColumnStyles.Add(aColumnTextColumn);
			}
			dataGrid2.TableStyles.Add(ts);
			//将结果显示在表格中
			dataGrid2[0,0]=isBalance;
			dataGrid2[0,1]=iniDebit.ToString();
			dataGrid2[0,2]=iniLoan.ToString();
			dataGrid2[0,3]=thisDebit.ToString();
			dataGrid2[0,4]=thisLoan.ToString();
			dataGrid2[0,5]=finDebit.ToString();
			dataGrid2[0,6]=finLoan.ToString();
			if(isBalance=="不平衡")//如果试算结果不平衡,则用红色来提醒用户
			{
				groupBox1.Text="试算结果不平衡!";
				groupBox1.BackColor=Color.Red;
			}
			else
			{
				groupBox1.Text="试算结果平衡";
			}

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -