column[0] column[1] column[2] total-
========== ========= ========= ======
row[1] 1 2 3
row[2] 1 0 1
total 2 2 4
我只能在第[0]栏获得值,但它不是从列[1]开始。我错过了什么?
Dim totalrow = ExportDT.NewRow
For Each col As Data.DataColumn In ExportDT.Columns
For n_Loop As Integer = 1 To ExportDT.Columns.Count - 1
Dim colname As Integer = col.ColumnName[n_loop]
totalrow(col.ColumnName) = Convert.ToDouble(ExportDT.Compute("SUM(
" & colname & ")", "nothing"))
Next
Next
ExportDT.Rows.Add(totalrow)
这是我的完整脚本。我仍然收到错误,无法在数据表中完成总计。
Dim objDB As New Database(AppSettings(" datasource"),AppSettings("数据库"),AppSettings("用户名"),AppSettings(&#34) ;密码&#34)) Dim TempComplaintCatDT As New Data.DataTable Dim TempProjectDT As New Data.DataTable Dim TempComplaintDT As New Data.DataTable Dim ExportDT As New Data.DataTable
objDB.OpenConnection()
objDB.dbVariable.SQLString = "SELECT CategoryUID,Category FROM aw_complaintcat ORDER BY Category"
objDB.FillData(TempComplaintCatDT, "ComplaintCat", Database.SQLCommandType.NormalString)
objDB.dbVariable.SQLString = "SELECT DISTINCT NameOfProject FROM aw_complaint ORDER BY NameOfProject"
objDB.FillData(TempProjectDT, "Projet", Database.SQLCommandType.NormalString)
objDB.dbVariable.SQLString = "SELECT ComplaintCategory, NameOfProject FROM aw_complaint"
objDB.FillData(TempComplaintDT, "Complaint", Database.SQLCommandType.NormalString)
objDB.CloseConnection()
'Using Excel for Generate the Report
ExportDT.Columns.Add("Category")
For Each ProjectRow As Data.DataRow In TempProjectDT.Rows
ExportDT.Columns.Add(ProjectRow.Item("NameOfProject").ToString.Trim)
Next
Dim NewRow As Data.DataRow
Dim ComplaintDV As New Data.DataView(TempComplaintDT)
Dim totalrow = ExportDT.NewRow
For Each CategoryRow As Data.DataRow In TempComplaintCatDT.Rows
NewRow = ExportDT.NewRow
'list all the category in table
NewRow.Item("Category") = CategoryRow.Item("Category").ToString
For nLoop As Integer = 1 To ExportDT.Columns.Count - 1
'count the project receive complaint
ComplaintDV.RowFilter = "ComplaintCategory='" & CategoryRow.Item("Category").ToString & "' AND NameOfProject='" & ExportDT.Columns(nLoop).ColumnName & "'"
If ComplaintDV.Count > 0 Then
NewRow.Item(ExportDT.Columns(nLoop).ColumnName) = ComplaintDV.Count
Else
NewRow.Item(ExportDT.Columns(nLoop).ColumnName) = "0"
End If
Next
For Each col As Data.DataColumn In ExportDT.Columns
For n_Loop As Integer = 0 To ExportDT.Columns.Count - 1
Dim colname As Integer = col.ColumnName
totalrow(col.ColumnName) = Convert.ToDouble(ExportDT.Compute("SUM(" & colname & ")", "ComplaintCategory=" & CategoryRow.Item("Category").ToString))
Next
Next
ExportDT.Rows.Add(totalrow)
ExportDT.Rows.Add(NewRow)
Next
ExportDT.Columns.Add("Total-")
ExportDT.Rows.Add("Total")