Search code examples
vb.netarraylistdata-bindingdevexpressxtrareport

Binding an Array List to a Report only yields one result


I am trying to create a report which takes data from an sql query as a data source using this as an example.

Everything runs smoothly and all 3 test records are put into the array list. However when the report shows only one records is being shown.

Can you please tell me what I am doing wrong? Thank you

Record Class:

Public Class RecordGastoDotacion
Dim _id, _usuario As Integer
Dim _cantidad As String
Dim _fecha, _hora As String
Dim _desc As String

Public Sub New(ByVal id As Integer, ByVal fecha As Date, ByVal hora As Date, ByVal cantidad As String, ByVal descripcion As String, ByVal usuario As Integer)
    Me._id = id
    Me._fecha = fecha.ToString("dd/MM/yyyy")
    Me._hora = hora.ToString("hh:mm:ss")
    Me._cantidad = cantidad
    Me._desc = descripcion
    Me._usuario = usuario
End Sub

Public Property idgasto() As Integer
    Get
        Return _id
    End Get
    Set(ByVal Value As Integer)
        _id = Value
    End Set
End Property

Public Property fef() As String
    Get
        Return _fecha
    End Get
    Set(ByVal Value As String)
        _fecha = Value
    End Set
End Property

Public Property feh() As String
    Get
        Return _hora
    End Get
    Set(ByVal Value As String)
        _hora = Value
    End Set
End Property

Public Property cant() As String
    Get
        Return _cantidad
    End Get
    Set(ByVal Value As String)
        _cantidad = Value
    End Set
End Property

Public Property descr() As String
    Get
        Return _desc
    End Get
    Set(ByVal Value As String)
        _desc = Value
    End Set
End Property

Public Property usuario() As Integer
    Get
        Return _usuario
    End Get
    Set(ByVal Value As Integer)
        _usuario = Value
    End Set
End Property
End Class

Report creation:

Cmd.CommandText = String.Format("SELECT gas.idre_gasto as idgasto, date(fe.fecha) as fef, time(fe.fecha) as feh, gas.cantidad as cant, gas.descripcion as descr, gas.re_usuario_idre_usuario as usuario FROM re_gasto as gas INNER JOIN re_corte_caja AS cor ON gas.re_corte_caja_idre_corte_caja = cor.idre_corte_caja
inner join re_fecha as fe on cor.re_fecha_idre_fecha = fe.idre_fecha WHERE date(fe.fecha) BETWEEN '{0:yyyy-MM-dd}' AND '{1:yyyy-MM-dd}' AND cor.estatus = {2}",
                                        DateTimePicker1.Value, DateTimePicker2.Value, ESTATUS_ACTIVO)
        rs = Cmd.Execute

        Dim listDataSource As New ArrayList()

        Do While Not rs.EOF
            listDataSource.Add(New RecordGastoDotacion(CInt(rs("idgasto").Value), CType(rs("fef").Value, Date), CType(rs("feh").Value, Date),
                                                       CType(rs("cant").Value, String), CType(rs("descr").Value, String),
                                                       CInt(rs("usuario").Value)))
            rs.MoveNext()
        Loop

        Dim gastos As New ReporteGastos() With {.Margins = New Printing.Margins(100, 100, 25, 25), .DataSource = listDataSource}

        gastos.XrLabel4.Text = String.Format("Día Creación: {0}", Now.ToString("dd/MM/yyyy"))
        gastos.XrLabel5.Text = String.Format("Hora Creación: {0}", Now.ToString("hh:MM"))


        gastos.AddBoundLabel("idgasto", New Rectangle(100, 20, 50, 30))
        gastos.AddBoundLabel("fef", New Rectangle(150, 20, 100, 30))
        gastos.AddBoundLabel("feh", New Rectangle(250, 20, 100, 30))
        gastos.AddBoundLabel("cant", New Rectangle(350, 20, 50, 30))
        gastos.AddBoundLabel("descr", New Rectangle(450, 20, 100, 30))
        gastos.AddBoundLabel("usuario", New Rectangle(550, 20, 50, 30))

        gastos.XrLabel12.Text = total

        Using printTool As New ReportPrintTool(gastos)
            printTool.ShowRibbonPreviewDialog(UserLookAndFeel.Default)
        End Using

ReporteGastos:

Imports System.Drawing
Imports DevExpress.XtraReports.UI

Public Class ReporteGastos
Public Sub AddBoundLabel(ByVal bindingMember As String, ByVal bounds As Rectangle)
    ' Create a label. 
    Dim label As New XRLabel()

    ' Add the label to the report's Detail band. 
    Detail.Controls.Add(label)

    ' Set its location and size. 
    label.Location = bounds.Location
    label.Size = bounds.Size

    ' Bind it to the bindingMember data field. 
    ' When the dataSource parameter is Nothing, the report's data source is used. 
    label.DataBindings.Add("Text", Nothing, bindingMember)
End Sub
End Class

Output:

Notice the total and its relation to what is being displayed


Solution

  • Refer the below documentation links to know that How binding of the ArrayList work with the XtraReport:
    How to: Bind a Report to an Array List
    Binding a Report to Lists
    How to: Bind a Report to a Collection that Implements the ITypedList Interface
    Providing Data to Reports

    From your current code it should display multiple records if ArrayList contains multiple items. Please read documentation for XtraReport related to datasource assignment..

    Check the example it is working as expected with your provided code snippet. It may be possible that you are some controls in report in wrong way. Please try to remove and add these added extra controls which are bind with the datasource one by one so that you can figure out control which causing problem.