遍历数据集并将 7 列中的 5 列的值应用到活动报表中的文本框

iterating through a dataset and apply values from 5 of 7 columns to textboxs in an active report

我想遍历数据集并将每个值应用于活动报表中的文本框。我不知道这些文本框是否需要成为 Group/Header 区域或什么。我知道我下面的代码只检索第一行。我如何遍历所有行并将数据应用到活动报告设法在组部分中获取多行的文本框

    Private Sub rptUserCellPhoneSwap_ReportStart(sender As Object, e As System.EventArgs) Handles Me.ReportStart
    Me.PageSettings.Orientation = GrapeCity.ActiveReports.Document.Section.PageOrientation.Landscape
    DateTxt.Text = Now.ToShortDateString & " " & Now.ToShortTimeString
    Dim DataSet = GrabInformation(FirstName, LastName)
    UserTxt.Text = LastName + ", " + FirstName

    'For Each dr As DataRow In DataSet.Tables(0).Rows
    '    OldIMEITxt.Text = DataSet.Tables(0).Rows(dr("OldIMEI")).ToString
    '    NewIMEITxt.Text = DataSet.Tables(0).Rows(dr("NewIMEI")).ToString
    '    ReasonTxt.Text = DataSet.Tables(0).Rows(dr("SwapReason")).ToString
    '    DateRepTxt.Text = DataSet.Tables(0).Rows(dr("DateSwapped")).ToString
    '    ValueTxt.Text = DataSet.Tables(0).Rows(dr("EstimatedAccumulatedValue")).ToString

    'Next

    If Not IsNothing(DataSet) Then
        If DataSet.Tables(0).Rows.Count > 0 Then
            OldIMEITxt.Text = DataSet.Tables(0).Rows(0)("OldIMEI")
            NewIMEITxt.Text = DataSet.Tables(0).Rows(0)("NewIMEI")
            ReasonTxt.Text = DataSet.Tables(0).Rows(0)("SwapReason")
            DateRepTxt.Text = DataSet.Tables(0).Rows(0)("DateSwapped")
            ValueTxt.Text = DataSet.Tables(0).Rows(0)("EstimateAccumulatedValue")
        End If
    End If



End Sub

这是循环获取所有行的方式,但在此示例中,文本框中唯一保留的数据将是最后一行。

如果你想连接指定文本框中的每一行信息,那么你应该有这样的信息。

OldIMEITxt.Text = OldIMEITxt.Text & dr("OldIMEI")

循环代码

For each dr as Datarow in DataSet.Tables(0).Rows
   OldIMEITxt.Text = dr("OldIMEI")
   NewIMEITxt.Text = dr("NewIMEI")
   ReasonTxt.Text = dr("SwapReason")
   DateRepTxt.Text = dr("DateSwapped")
   ValueTxt.Text = dr("EstimateAccumulatedValue")
Next

A​​ctiveReports 可以从您的 DataSet 中读取数据,而无需在代码中添加额外的循环。 如果您 return 数据 table 到报表对象的 DataSource 属性,那么正确设置 TextBox 控件和 GroupHeader 部分的 DataField 属性 就足以在报表中显示数据。渲染引擎将自动遍历所有数据行:

    Private Sub SectionReport1_ReportStart(sender As Object, e As EventArgs) Handles MyBase.ReportStart
    ' bind TextBox controls to fields in table
    Me.txtF1.DataField = "F1"
    Me.txtF2.DataField = "F2"
    ' set the grouping field
    Me.GroupHeader1.DataField = "F2"
    ' set the report data source
    Me.DataSource = GetSampleData().Tables(0)
End Sub

Private Function GetSampleData() As DataSet
    Dim ds = New DataSet()
    Dim dt = ds.Tables.Add("TestData")
    dt.Columns.Add("F1")
    dt.Columns.Add("F2")
    dt.Rows.Add("1", "0")
    dt.Rows.Add("2", "0")
    dt.Rows.Add("1", "1")
    dt.Rows.Add("2", "1")
    Return ds
End Function

如果您更喜欢在 "semi-automatic mode" 中逐行读取数据,那么 FetchData 事件处理程序可以在此处提供帮助:

    Dim i As Integer
Dim dt As DataTable = Nothing

Private Sub SectionReport2_ReportStart(sender As Object, e As EventArgs) Handles MyBase.ReportStart
    i = 0
    ' bind TextBox controls to fields in table
    Me.txtF1.DataField = "F1"
    Me.txtF2.DataField = "F2"
    ' set the grouping field
    Me.GroupHeader1.DataField = "F2"
    dt = GetSampleData().Tables(0)
End Sub

Private Sub SectionReport2_DataInitialize(sender As Object, e As EventArgs) Handles MyBase.DataInitialize
    Me.Fields.Add("F1")
    Me.Fields.Add("F2")
End Sub

Private Sub SectionReport2_FetchData(sender As Object, eArgs As FetchEventArgs) Handles MyBase.FetchData
    If dt.Rows.Count > i Then
        Me.Fields("F1").Value = dt.Rows(i)(0)
        Me.Fields("F2").Value = dt.Rows(i)(1)
        eArgs.EOF = False
    Else
        eArgs.EOF = True
    End If
    i = i + 1
End Sub

Private Function GetSampleData() As DataSet
    Dim ds = New DataSet()
    Dim _dt = ds.Tables.Add("TestData")
    _dt.Columns.Add("F1")
    _dt.Columns.Add("F2")
    _dt.Rows.Add("1", "0")
    _dt.Rows.Add("2", "0")
    _dt.Rows.Add("3", "1")
    _dt.Rows.Add("4", "1")
    Return ds
End Function

此外,我建议查看 ActiveReports 安装包中带有 运行 时间数据绑定的示例。 这是官方网站上示例描述的 link: Unbound Data