2016-02-09 50 views
0

我是新編程的晶體報告,請幫助我。我有關於它的分報告的主要報告。我已經知道如何設置主要報告的數據源,問題出在我的子報告中。以下是我做我的主報告:如何使用vb.net設置子報表水晶報表的數據源?

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 

這就是我如何生成的數據,並將其設置爲Crystal報表,現在的問題是分報告,我不知道如何設置數據源,我想重複這一點,將其設置爲下一個表(DataGridView中),並創建另一個數據表,但它顯示空..我試圖rptDoc.OpenSubreport(0).SetDataSource(dtable2)

希望有人明白我的問題..謝謝提前

+0

幫你一個忙,並使用[SqlDataAdapter](https://msdn.microsoft.com/en-US/library/system.data.sqlclient.sqldataadapter%28v=vs.110%29.aspx)來直接加載數據表。您可以按照該鏈接中的示例進行操作,但將DataTable替換爲使用DataSet的位置。 – TnTinMn

回答

0

它可能是有點簡單如果你這樣試試

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

我試過這個,它給我看了一個空白頁面 –

+0

比你需要檢查水晶報告,
你有沒有報告和子報告有正確的鏈接,也有你的數據源中傳遞相同的鏈接?即rptDoc.SetDataSource(dtable)和rptDoc.SubReports(0).SetDataSource(dtTable1) – Nirav

+0

我不知道爲什麼標籤不工作:( – Nirav