簡體   English   中英

將數據網格視圖轉換為報表查看器

[英]Convert Data Grid View to Report Viewer

紳士,

在構造此問題之前,我閱讀了30多個搜索條目(並嘗試了所有搜索條目),其中大多數沒有回復。 盡管這是我第一次在這里提出問題,但這是我十多年來最喜歡獲得答案的地方。 這樣一來...我創建了以下代碼,以從另一頁上的數據集中接受數據,並將其傳播到報表查看器中。

    //Creates the Grid View
        dataGridViewReport.DataSource = null;            
        dataGridViewReport.DataSource = ds.Tables[0].DefaultView;

        //Creates the Report
        reportViewer1.LocalReport.DataSources.Clear();
        this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @" \Report1.rdlc";// @"C:\Users\Pat\Documents\Visual Studio 2010\Projects\TTW 151200\TTW\Report1.rdlc";
        DataTable dt = ds.Tables[0];
        dt.TableName = "DataSet1";
        ReportDataSource rds = new ReportDataSource("DataSet1", dataGridViewReport.DataSource);
        reportViewer1.LocalReport.DataSources.Add(rds);            
        reportViewer1.RefreshReport();

頁面加載后,此頁面接受來自構造函數的數據集值,並在上面的代碼中使用它。 到目前為止,這一直是一個磨難,但我認為這可能已經接近。 我認為問題可能是找不到報告文件,盡管它不再引發該錯誤。 現在,當我創建報告文件(Report1.rdlc)時,它與其他表單文件一起出現。 我想讀的是,它將在運行時位於/ bin或/ release文件夾中,但似乎不存在,也無法將其復制到該位置。 我找不到實際路徑在哪里,只能找到未運行的路徑。 所以...首先,我不確定我是否具有正確的代碼來創建報告,但不再拋出任何錯誤。 視圖顯示僅表明無法找到該文件。 其次,我應該指向哪里,以便此代碼(或任何代碼)具有指向文件的正確路徑,以便可以創建報告。 最后,我不理解需要數據集名稱的參數,因此我只是從一些示例中復制了一些示例代碼,這些示例據說可以正常工作。 我在屬性表中將“生成操作”更改為“嵌入式資源”,還將“復制到輸出復制為始終復制”(盡管這對我而言從來沒有用)。 請幫忙。 一個簡單,清晰的代碼段將大大幫助我恢復理智! 謝謝!

一旦使用安裝程序安裝了應用程序,將ReportPath設置為Application.StartupPath + <report name> (最好使用由字符串連接組成的Path.Combine )可能是正確的。

但是在調試過程中,您必須考慮以下幾點:

  • 編譯文件( exedll等)位於bin文件夾( binbin\\Debugbin\\Release )中
  • rdlc文件保留在原始項目文件夾中

因此,您可以實現執行以下操作的功能(抱歉,VB.NET代碼):

Private Function strRdlcFilePath(ByVal strRdlcFile As String) As String

    If Debugger.IsAttached And IO.File.Exists(IO.Path.Combine("<path to your project folder>", strRdlcFile)) Then
        strRdlcFilePath = IO.Path.Combine("<path to your project folder>", strRdlcFile)
    ElseIf Not Debugger.IsAttached And IO.File.Exists(IO.Path.Combine(Application.StartupPath, strRdlcFile)) Then
        strRdlcFilePath = IO.Path.Combine(Application.StartupPath, strRdlcFile)
    Else
        'worst case: use an OpenFileDialog to manually select your rdlc file
        strRdlcFilePath = "YourOpenFileDialog.FileName"
    End If

End Function

為了將ReportPath設置為:

reportViewer1.LocalReport.ReportPath = strRdlcFilePath("Report1.rdlc")

關於數據集問題,我認為您的代碼是正確的,但事先聲明了DataTable最好使用:

ReportDataSource rds = new ReportDataSource(dt.TableName, dt);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM