sql-server - DataGridView 未显示表 VB.NET SqlServer 中的所有记录
问题描述
我有一个表 SalariesSettingsForEmployee ,其中保存了员工津贴和扣除额。
所有类型的津贴和扣除都加载到 GridView 列和 Gridview 行中的员工中。(具有各自的值)
问题是数据仅加载到一个(或第一个)员工的网格视图中,而不是全部。
我做了两个子程序。这是我的代码。
如果我在这里做错了什么,请告知。
谢谢并恭祝安康,
Sub CreateEmployeeRows()
Dim subtotal As Double = 0
Dim NetTotal As Double = 0
Dim tempval As Double = 0
Dim rno As Integer = 0
Dim BasicSalary As Double = 0
Dim DeductionTotal As Double = 0
Dim GrandTotal As Double = 0
BasicSalary = SQLGetNumericFieldValue("Select Amount From SalariesSettingsForEmployee Where AllowanceName='BASIC SALARY'", "Amount")
Dim dr As DataRow = dt.NewRow
Dim sqlcmmd As New SqlClient.SqlCommand
Try
sqlcmmd.Connection = DAL.OpenSqlConnection
sqlcmmd.CommandText = "Select * From SalariesSettingsForEmployee Where AllowanceType='Allowance'"
Dim sreader As SqlClient.SqlDataReader
sreader = sqlcmmd.ExecuteReader
While sreader.Read
subtotal = BasicSalary
dr(0) = sreader("EmployeeCode").ToString
dr(1) = sreader("EmployeeName").ToString
dr(2) = sreader("BranchCode")
dr(3) = sreader("BranchName")
If sreader("AmountType").ToString = "Fixed Amount" Then
tempval = sreader("Amount")
subtotal = subtotal + tempval
ElseIf sreader("AmountType").ToString = "Percentage" Then
tempval = subtotal * CDbl(sreader("Amount")) / 100
subtotal = subtotal + tempval
End If
dr(sreader("AllowanceName")) = tempval
''sreader("Amount")
GrandTotal = subtotal
dr(GrossTotalColName) = GrandTotal
dt.Rows.Add(dr)
txtRunPayrollGird.DataSource = dt
End While
sreader.Close()
sreader = Nothing
DAL.CloseSQLConnection()
Catch ex As Exception
End Try
'End of Allowance Totals
'Calculate Deduction Totals
Dim cmd As New SqlClient.SqlCommand
Try
cmd.Connection = DAL.OpenSqlConnection
cmd.CommandText = "Select * From SalariesSettingsForEmployee Where AllowanceType='Deduction'"
Dim rdr As SqlClient.SqlDataReader
rdr = cmd.ExecuteReader
While rdr.Read
tempval = 0
If rdr("AmountType").ToString = "Fixed Amount" Then
tempval = rdr("Amount")
subtotal = subtotal + tempval
DeductionTotal = DeductionTotal + tempval
ElseIf rdr("AmountType").ToString = "Percentage" Then
tempval = GrandTotal * CDbl(rdr("Amount")) / 100
subtotal = subtotal + tempval
End If
dr(rdr("AllowanceName")) = tempval
''sreader("Amount")
dr(NetSalaryColName) = GrandTotal - DeductionTotal
NetTotal = NetTotal + (GrandTotal - DeductionTotal)
txtNetPayable.Text = NetTotal
dt.Rows.Add(dr)
txtRunPayrollGird.DataSource = dt
End While
Catch ex As Exception
End Try
End Sub
Sub CreateColumns()
Dim Sqlcmmd As New SqlClient.SqlCommand
Dim Sreader As SqlClient.SqlDataReader
Try
dt = New DataTable
dt.Columns.Add("Employee ID")
dt.Columns.Add("Employee Name")
dt.Columns.Add("Branch Code")
dt.Columns.Add("Branch Name")
'For Additions
Sqlcmmd.Connection = DAL.OpenSqlConnection
Sqlcmmd.CommandText = "SELECT * FROM SalariesPaySettings WHERE AllowanceType='Allowance'"
Sqlcmmd.CommandType = CommandType.Text
Sreader = Sqlcmmd.ExecuteReader
While Sreader.Read
dt.Columns.Add(Sreader("AllowanceName").ToString.Trim)
End While
dt.Columns.Add(GrossTotalColName)
Sreader.Close()
Sreader = Nothing
DAL.CloseSQLConnection()
Sqlcmmd.Connection = Nothing
Catch ex As Exception
End Try
'For Deductions
Try
Dim Sqlcmmd1 As New SqlClient.SqlCommand
Sqlcmmd1.Connection = DAL.OpenSqlConnection
Sqlcmmd1.CommandText = "SELECT * FROM SalariesPaySettings WHERE AllowanceType='Deduction'"
Sqlcmmd1.CommandType = CommandType.Text
Dim Sreader1 As SqlClient.SqlDataReader
Sreader1 = Sqlcmmd1.ExecuteReader
While Sreader1.Read
dt.Columns.Add(Sreader1("AllowanceName").ToString.Trim)
End While
dt.Columns.Add(NetSalaryColName)
txtRunPayrollGird.DataSource = dt
Sreader1.Close()
Sreader1 = Nothing
DAL.CloseSQLConnection()
Catch ex As Exception
End Try
'
''''
解决方案
问题是您正在DataRow
这条线上创建一个单曲:
Dim dr As DataRow = dt.NewRow
并在循环中重用该行,覆盖在先前迭代中写入该行的值。您需要做的是为DataRow
循环的每次迭代创建一个新的。
在您的第一个循环中:
While sreader.Read
' Create new row.
Dim dr As DataRow = dt.NewRow
subtotal = BasicSalary
同样对于第二个循环:
While rdr.Read
tempval = 0
' Create new row.
Dim dr As DataRow = dt.NewRow
If rdr("AmountType").ToString = "Fixed Amount" Then
推荐阅读
- swift - 当我不断增加它们的大小时,我试图让我的按钮居中?
- css - 当我关闭 Chrome devtools 中的 CSS 规则然后再次打开时,绝对定位的元素会改变位置
- wordpress - 我的客户需要域和托管在不同的托管服务提供商
- asp.net-mvc - Syncfusion MVC Datagrid - 如何在行中显示按钮
- javascript - node.js、express.js 和 mysql - 无法读取未定义的属性“状态”
- javascript - {{}} 我在 html 脚本标签中找到了这个
- mysql - 如何在 2 个表中进行计数
- spring-cloud-dataflow - 自定义处理器流程的 Http 引发 org.springframework.expression.spel.SpelEvaluationException: EL1004E
- wso2 - 如何通过 WSO2-API 管理器传递 CKAN API 授权令牌
- android - 如何从firestore中的数组数据字段中获取数据?