📄 frmpingheng.vb
字号:
Public Class frmPingHeng
Inherits System.Windows.Forms.Form
Private dtTop As DataTable
Private dtBottom As DataTable
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents dgdTop As System.Windows.Forms.DataGrid
Friend WithEvents dgdBottom As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmPingHeng))
Me.dgdTop = New System.Windows.Forms.DataGrid
Me.dgdBottom = New System.Windows.Forms.DataGrid
CType(Me.dgdTop, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.dgdBottom, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dgdTop
'
Me.dgdTop.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.dgdTop.CaptionVisible = False
Me.dgdTop.DataMember = ""
Me.dgdTop.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgdTop.Location = New System.Drawing.Point(0, 0)
Me.dgdTop.Name = "dgdTop"
Me.dgdTop.Size = New System.Drawing.Size(448, 208)
Me.dgdTop.TabIndex = 0
'
'dgdBottom
'
Me.dgdBottom.CaptionVisible = False
Me.dgdBottom.DataMember = ""
Me.dgdBottom.Dock = System.Windows.Forms.DockStyle.Bottom
Me.dgdBottom.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgdBottom.Location = New System.Drawing.Point(0, 209)
Me.dgdBottom.Name = "dgdBottom"
Me.dgdBottom.Size = New System.Drawing.Size(448, 64)
Me.dgdBottom.TabIndex = 1
'
'frmPingHeng
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(448, 273)
Me.Controls.Add(Me.dgdBottom)
Me.Controls.Add(Me.dgdTop)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "frmPingHeng"
Me.Text = "试算平衡表"
CType(Me.dgdTop, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.dgdBottom, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub SetTopDataGrid()
Dim db As DataBase = New DataBase
Dim strSQL As String = "select 科目代码,科目名称,(case"
strSQL += " when 余额方向='借方' then 期初余额 else 0 end -"
strSQL += " 累计借方) as 期初借方,(case when 余额方向='贷方'"
strSQL += " then 期初余额 else 0 end - 累计贷方) as 期初贷方,"
strSQL += "累计借方 as 本期发生借方,累计贷方 as 本期发生贷方,"
strSQL += "case when 余额方向='借方' then 期初余额 else 0 end as "
strSQL += "期末借方,case when 余额方向='贷方' then 期初余额 else 0"
strSQL += " end as 期末贷方 from 帐簿初始化表 where 累计借方<>0 or"
strSQL += " 累计贷方<>0 or 期初余额<>0"
dtTop = db.RunSelectSQL(strSQL).Table
Dim ts As New DataGridTableStyle
Dim aColumnTextColumn As DataGridTextBoxColumn
Dim numCols As Integer = dtTop.Columns.Count
Dim i As Integer
For i = 0 To numCols - 1
aColumnTextColumn = New DataGridTextBoxColumn
aColumnTextColumn.MappingName = _
dtTop.Columns(i).ColumnName
aColumnTextColumn.HeaderText = _
dtTop.Columns(i).ColumnName
aColumnTextColumn.NullText = ""
aColumnTextColumn.Width = 60
aColumnTextColumn.ReadOnly = True
ts.GridColumnStyles.Add(aColumnTextColumn)
Next
ts.AlternatingBackColor = Color.LightGray
ts.AllowSorting = False
ts.MappingName = dtTop.TableName
dgdTop.TableStyles.Add(ts)
Dim dv As DataView = dtTop.DefaultView
dv.AllowNew = False
dv.AllowDelete = False
dv.AllowEdit = False
'设置数据源
dgdTop.DataSource = dtTop.DefaultView
End Sub
Private Sub frmPingHeng_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetTopDataGrid()
CreateDataTalbe()
CalPingHeng(0)
SetBottomDataGrid()
End Sub
Private Sub CreateDataTalbe()
'创建数据表
dtBottom = New DataTable("结果")
'添加列
With dtBottom.Columns
.Add("是否平衡", Type.GetType("System.String"))
.Add("期初借方合计", Type.GetType("System.String"))
.Add("期初贷方合计", Type.GetType("System.String"))
.Add("本期借方合计", Type.GetType("System.String"))
.Add("本期贷方合计", Type.GetType("System.String"))
.Add("期末借方合计", Type.GetType("System.String"))
.Add("期末贷方合计", Type.GetType("System.String"))
End With
End Sub
' 计算是否平衡并显示出计算结果
' nStatus 0 初始化帐簿时计算
' 1 正式使用后凭证的计算
Public Function CalPingHeng(ByVal iStatus As Integer) As Boolean
Dim bResult As Boolean
' 根据不同的状态计算平衡情况
Dim strSQL As String
' 初始化帐簿的试算平衡,需要根据初始化的信息反算期初
Dim db As DataBase = New DataBase
If iStatus = 0 Then
strSQL = "select 科目代码, 科目名称,"
strSQL += "(case when 余额方向='借方' then 期初余额 "
strSQL += " else 0 end - 累计借方) as 期初借方,"
strSQL += "(case when 余额方向='贷方' then 期初余额"
strSQL += " else 0 end - 累计贷方) as 期初贷方,"
strSQL += "累计借方 as 本期发生借方,累计贷方 as 本期发生贷方,"
strSQL += "case when 余额方向='借方' then 期初余额 "
strSQL += "else 0 end as 期末借方,"
strSQL += "case when 余额方向='贷方' then 期初余额 "
strSQL += " else 0 end as 期末贷方 "
strSQL += " from 帐簿初始化表 "
strSQL += " where 累计借方<>0 or 累计贷方<>0 or 期初余额<>0"
dgdTop.DataSource = db.RunSelectSQL(strSQL)
' 计算合计金额,用于判断是否平衡
strSQL = "select "
strSQL += "sum(case when 余额方向='借方' "
strSQL += " then 期初余额 else 0 end-累计借方) as 期初借方合计,"
strSQL += "sum(case when 余额方向='贷方' "
strSQL += " then 期初余额 else 0 end-累计贷方) as 期初贷方合计,"
strSQL += "sum(累计借方) as 本期借方合计,sum(累计贷方) "
strSQL += "as 本期贷方合计,"
strSQL += "sum(case when 余额方向='借方' then "
strSQL += "期初余额 else 0 end) as 期末借方合计,"
strSQL += "sum(case when 余额方向='贷方' then "
strSQL += " 期初余额 else 0 end) as 期末贷方合计"
strSQL += " from 帐簿初始化表 "
strSQL += " where 累计借方<>0 or 累计贷方<>0 or 期初余额<>0"
Dim dv As DataView = db.RunSelectSQL(strSQL)
bResult = AddToDataTable(dv)
Else
strSQL = "select a.科目代码, b.科目名称,"
strSQL += "(case when a.余额方向='借方' "
strSQL += " then 期初余额 else 0 end ) as 期初借方,"
strSQL += "(case when a.余额方向='贷方' "
strSQL += " then 期初余额 else 0 end) as 期初贷方,"
strSQL += "本期借方合计 as 本期发生借方, 本期贷方合计 "
strSQL += "as 本期发生贷方,"
strSQL += "case when a.余额方向='借方' then 余额 else 0 end "
strSQL += "as 期末借方,"
strSQL += "case when a.余额方向='贷方' then 余额 else 0 end "
strSQL += "as 期末贷方"
strSQL += " from 本期汇总账簿 as a, 科目表 as b "
strSQL += " where a.科目代码 = b.科目代码 and (本期借方合计<> 0"
strSQL += " or 本期贷方合计<>0 or 期初余额<>0 or 余额<>0)"
Dim dvTemp As DataView = db.RunSelectSQL(strSQL)
dvTemp.AllowEdit = False
dvTemp.AllowDelete = False
dvTemp.AllowNew = False
dgdTop.DataSource = dvTemp
strSQL = "select "
strSQL += "sum(case when 余额方向='借方' then 期初余额 else 0 end)"
strSQL += " as 期初借方合计,"
strSQL += "sum(case when 余额方向='贷方' then 期初余额 else 0 end)"
strSQL += " as 期初贷方合计,"
strSQL += "sum(本期借方合计) as 本期借方合计,sum(本期贷方合计) "
strSQL += "as 本期贷方合计,"
strSQL += "sum(case when 余额方向='借方' then 余额 else 0 end) "
strSQL += "as 期末借方合计,"
strSQL += "sum(case when 余额方向='贷方' then 余额 else 0 end) "
strSQL += "as 期末贷方合计"
strSQL += " from 本期汇总账簿 "
strSQL += " where 本期借方合计<> 0 or 本期贷方合计<>0 or "
strSQL += "期初余额<>0 or 余额<>0"
Dim dv As DataView = db.RunSelectSQL(strSQL)
bResult = AddToDataTable(dv)
Me.dgdBottom.DataSource = dtBottom
End If
db.Dispose()
Return bResult
End Function
Private Sub SetBottomDataGrid()
Dim ts As New DataGridTableStyle
Dim aColumnTextColumn As DataGridTextBoxColumn
Dim numCols As Integer = dtBottom.Columns.Count
Dim i As Integer
For i = 0 To numCols - 1
aColumnTextColumn = New DataGridTextBoxColumn
aColumnTextColumn.MappingName = _
dtBottom.Columns(i).ColumnName
aColumnTextColumn.HeaderText = _
dtBottom.Columns(i).ColumnName
aColumnTextColumn.NullText = ""
aColumnTextColumn.Width = 80
aColumnTextColumn.ReadOnly = True
ts.GridColumnStyles.Add(aColumnTextColumn)
Next
ts.AlternatingBackColor = Color.LightGray
ts.AllowSorting = False
ts.MappingName = dtBottom.TableName
dgdBottom.TableStyles.Add(ts)
Dim dv As DataView = dtBottom.DefaultView
dv.AllowNew = False
dv.AllowDelete = False
dv.AllowEdit = False
'设置数据源
dgdBottom.DataSource = dtBottom.DefaultView
End Sub
Private Function AddToDataTable(ByRef dv As DataView) As Boolean
'判断dtBottom是否初始化
If dtBottom Is Nothing Then
CreateDataTalbe()
End If
Dim bResult As Boolean
'清空数据表的内容
dtBottom.Rows.Clear()
Dim dr As DataRow = dtBottom.NewRow()
'获取一行数据
Try
dr("期初借方合计") = SetText(dv.Item(0)("期初借方合计"))
dr("期初贷方合计") = SetText(dv.Item(0)("期初贷方合计"))
dr("本期借方合计") = SetText(dv.Item(0)("本期借方合计"))
dr("本期贷方合计") = SetText(dv.Item(0)("本期贷方合计"))
dr("期末借方合计") = SetText(dv.Item(0)("期末借方合计"))
dr("期末贷方合计") = SetText(dv.Item(0)("期末贷方合计"))
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
dr("期初借方合计") = SetText(dv.Item(0)("期初借方合计"))
'判断是否平衡
If SetText(dv.Item(0)("期初借方合计")) _
<> SetText(dv.Item(0)("期初贷方合计")) Or _
SetText(dv.Item(0)("本期借方合计")) _
<> SetText(dv.Item(0)("本期贷方合计")) Or _
SetText(dv.Item(0)("期末借方合计")) _
<> SetText(dv.Item(0)("期末贷方合计")) Then
dr("是否平衡") = "不平衡"
bResult = False
Else
dr("是否平衡") = "平衡"
bResult = True
End If
dtBottom.Rows.Add(dr)
Return bResult
End Function
Private Function SetText(ByVal obj As Object) As String
If obj Is Nothing Then
Return ""
Else
Return obj.ToString()
End If
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -