迭代数据集并将7列中的5列中的值应用于活动报表中的文本框

时间:2016-11-04 13:16:43

标签: vb.net activereports

我想迭代一个dataSet并将每个值应用到活动报表中的文本框中。我不知道这些文本框是否需要是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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

ActiveReports可以从您的DataSet中读取数据,而无需在代码中添加其他循环。 如果将数据表返回到报表对象的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

如果您希望在“半自动模式”中逐行读取数据,那么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安装包中查看带有运行时数据绑定的示例。 这里是官方网站上的示例说明的链接: Unbound Data