Search code examples
vb.netactivereports

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


I want to iterate through a dataSet and apply each value to a textbox in an active report. i dont know if these text boxes need to be a the Group/Header area or what. i know that my code below is only retrieving the first row. how can I iterate through all rows and apply the data to text boxes that active reports manages to get multiple rows in the group section

    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

Solution

  • ActiveReports can read data from your DataSet without additional loops in code. if you return the data table to DataSource property of report object, then it is enough to set DataField property of TextBox controls and GroupHeader section correctly to show data in report. the rendering engine will go through all data rows automatically:

        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
    

    if you prefer to read data row by row in "semi-automatic mode", then FetchData event handler can help here:

        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
    

    also, i would recommend to look at the sample with run time data binding in the ActiveReports installation package. here is a link to the sample description on the official site: Unbound Data