2012-01-10 10 views
8

Ho creato un controllo personalizzato (un modulo di Windows con un visualizzatore di report). Ho il seguente codice per caricare un report locale:Impostazione dell'origine dati per un report locale - .NET e Report Viewer

Contenuto in CustomReportViewer Classe

//Load local report 
this.reportViewer1.ProcessingMode = ProcessingMode.Local;   
//enable loading of external images   
this.reportViewer1.LocalReport.EnableExternalImages = true; 
//pass the report to the viewer 
using (FileStream stream = new FileStream(filename, FileMode.Open)) 
{ 
    this.reportViewer1.LocalReport.LoadReportDefinition(stream); 
} 

Io chiamo questo utilizzando:

CustomReportViewer reportViewer = new CustomReportViewer(); 

Questo funziona bene e un Windows Form appare contenente la report controllo spettatore ma Ricevo il seguente messaggio:

A data source instance has not been supplied for the data source "ReportData" 

Non sono completamente sicuro di come impostare l'origine dati? I dati richiesti sono archiviati in un database remoto ... cosa devo fare per impostare questa connessione?

risposta

16

È necessario creare un ReportDataSource e impostare la proprietà Value - ad es. DataTable e IEnumerable s sono supported sources

A titolo di esempio, e supponendo che un metodo esiste per restituire un DataSet, con un unico DataTable corrispondere le colonne necessarie per il report:

DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. via DataAdapter 
// If your report needs parameters, they need to be set ... 
ReportParameter[] parameters = new ReportParameter[...]; 

ReportDataSource reportDataSource = new ReportDataSource(); 
// Must match the DataSource in the RDLC 
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = ds.Tables[0]; 

// Add any parameters to the collection 
reportViewer1.LocalReport.SetParameters(parameters); 
reportViewer1.LocalReport.DataSources.Add(reportDataSource); 
reportViewer1.DataBind(); 

Si noti che spesso è più facile per incorporare semplicemente l'RDLC nell'assieme, anziché dover conservare file RDLC separati. A tale scopo, selezionando il Build Action sul RDLC come Embedded Resource, e quindi è possibile impostare la proprietà ReportEmbeddedResource:

reportViewer1.LocalReport.ReportEmbeddedResource = 
         "MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc"; 

Si noti che la stringa di risorsa deve includere il nome completo della risorsa (compreso il montaggio).

+0

noti inoltre se le risorse sono in una cartella, che il nome della cartella ottiene anche nel nome completo. – StuartLC

7
La chiave per me è stata la risposta di StuartLC come sopra ... con ulteriore chiarimento in quanto quando ha detto "Deve corrispondere al DataSource nell'RDLC" .. in realtà si è rivelato essere il valore dell'elemento "DataSetName" re: <DataSetName>DataSet1</DataSetName>

Sono andato in tondo perché è chiamato "DataSource", quindi ho continuato a utilizzare il nome dell'elemento DataSource ma a quanto pare nel file rdl e rdlc questo in realtà significa il DataSetName. Tenendo questo in mente ecco il codice preso in prestito da Stuart sopra il mio. Si noti il ​​valore dell'elemento DataSetName:

 using (SqlConnection sqlConn = new SqlConnection(rvConnection)) 
     using (SqlDataAdapter da = new SqlDataAdapter(rvSQL, rvConnection)) 
     { 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      DataTable dt = ds.Tables[0]; 

      this.reportViewer1.Reset(); 
      this.reportViewer1.ProcessingMode = ProcessingMode.Local; 
      this.reportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "ssrsExport.rdlc"; 
      ReportDataSource reportDataSource = new ReportDataSource(); 
      // Must match the DataSet in the RDLC 
      reportDataSource.Name = "DataSet1"; 
      reportDataSource.Value = ds.Tables[0]; 
      this.reportViewer1.LocalReport.DataSources.Add(reportDataSource); 
      this.reportViewer1.RefreshReport(); 
     } 
Problemi correlati