VS2010报告查看器的空月总数

时间:2015-10-20 15:25:30

标签: sql visual-studio reportviewer

我有一张包含销售数据的表格,需要使用VS2010报告查看器生成报告。我可以按月进行基本分组,但需要一种方法来插入包含没有活动的月份的记录。输入表是:

Input table

报告应该显示如下内容:

Month                Sales
Jan                  1303
Feb                  250
Mar                  0
Apr                  500

非常感谢任何建议或建议的文章。

罗布

1 个答案:

答案 0 :(得分:0)

您可以在将数据用作ReportDataSource之前操作VS2010中的数据。

Dim dtTest As New DataTable
dtTest.Columns.Add(New DataColumn("Date", GetType(System.DateTime)))
dtTest.Columns.Add(New DataColumn("Item", GetType(System.String)))
dtTest.Columns.Add(New DataColumn("Value", GetType(System.Double)))

dtTest.Rows.Add(New DateTime(2015, 1, 13), "item1", 1200)
dtTest.Rows.Add(New DateTime(2015, 1, 20), "item1", 103)
dtTest.Rows.Add(New DateTime(2015, 2, 9), "item1", 250)
dtTest.Rows.Add(New DateTime(2015, 4, 2), "item1", 500)
dtTest.Rows.Add(New DateTime(2015, 1, 5), "item2", 100)
dtTest.Rows.Add(New DateTime(2015, 1, 30), "item2", 10)
dtTest.Rows.Add(New DateTime(2015, 2, 3), "item2", 20)
dtTest.Rows.Add(New DateTime(2015, 3, 7), "item2", 50)

'report data source
Dim dtReportDataSource As New DataTable

Dim dcYear As New DataColumn("Year", GetType(System.Int32))
Dim dcMonth As New DataColumn("Month", GetType(System.Double))
Dim dcItem As New DataColumn("Item", GetType(System.String))
Dim dcSum As New DataColumn("Sum", GetType(System.Double))

dtReportDataSource.Columns.Add(dcYear)
dtReportDataSource.Columns.Add(dcMonth)
dtReportDataSource.Columns.Add(dcItem)
dtReportDataSource.Columns.Add(dcSum)

dtReportDataSource.Columns("Sum").DefaultValue = 0
dtReportDataSource.PrimaryKey = New DataColumn() {dcYear, dcMonth, dcItem}

Dim dtStartDate As DateTime = CDate(dtTest.Select("", "Date")(0)("Date"))
Dim dtEndDate As DateTime = CDate(dtTest.Select("", "Date DESC")(0)("Date"))

For i As Integer = 0 To DateDiff(DateInterval.Month, dtStartDate, dtEndDate)

    Dim intYear As Integer = DateAdd(DateInterval.Month, i, dtStartDate).Year
    Dim intMonth As Integer = DateAdd(DateInterval.Month, i, dtStartDate).Month

    For Each drTest As DataRow In New DataView(dtTest).ToTable(True, "Item").Rows
        dtReportDataSource.Rows.Add(intYear, intMonth, drTest("Item"))
    Next drTest

Next i

For Each drTest As DataRow In dtTest.Rows

    Dim drReportDataSource As DataRow = dtReportDataSource.Rows.Find(New Object() {CDate(drTest("Date")).Year, CDate(drTest("Date")).Month, drTest("Item")})

    If Not IsNothing(drReportDataSource) Then drReportDataSource("Sum") += drTest("Value")

Next drTest

YouReportViewer.LocalReport.DataSources.Add(New ReportDataSource("Test", dtReportDataSource))