简体   繁体   中英

How to set data source of sub-reports crystal report using vb.net?

I am new in programming crystal Report, so please help me. i have main report with sub-report on it. i know already how to set data source of main report, the problem is in my sub-report. here's how i did for my main report:

Try
    open_con()
    Dim a As String
    a = "SELECT * FROM students where year_id=1"
    Dim reader As SqlDataReader
    cmd = New SqlCommand(a, dbcon)
    reader = cmd.ExecuteReader

    'tblstud is a datagridview

    While reader.Read
        tblstud.Rows.Add(reader("name"), reader("address"),reader("grade_level"),reader("id_number"))
    End While
    reader.Close()
    cmd.Dispose()
    close_con()
 Catch ex As Exception
    MsgBox(ex.Message)
 End Try

 Dim dtable As New DataTable
 With dtable
     .Columns.Add("name")
     .Columns.Add("address")
     .Columns.Add("g_lvl")
     .Columns.Add("id_num")
 End With
 For Each tblrow As DataGridViewRow In Me.tblstud.Rows
     dtable.Rows.Add(tblrow.Cells("names").Value, tblrow.Cells("addrs").Value, tblrow.Cells("lvl").Value, tblrow.Cells("ids").Value)
 Next

Try
    Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
    rptDoc = New stud_rep
    rptDoc.SetDataSource(dtable)
    CrystalReportViewer1.ReportSource = rptDoc
Catch ex As Exception
    MsgBox(ex.Message)
End Try

that's how i generated data and set it to the Crystal report, now the problem is in sub-report i don't know how to set data source, i tried to repeat that and set it to the next table (datagridview) and create another datatable but it shows empty.. i tried rptDoc.OpenSubreport(0).SetDataSource(dtable2)

hope anybody understand my problem.. thanks in advance

如果您这样尝试可能会有点简单

RptDocument.Subreports[0].SetDataSource(YourDataSource);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM