2013-07-12 108 views
0

當設計時report-XmlFile(* .rdlc)沒有數據源/數據集定義時,是否有任何方式以編程方式將數據源/數據集添加到Microsoft.Reporting.WebForms.LocalReport中?以編程方式將數據源/數據集添加到LocalReport

這工作,如果我已經在我的* .rdlc

C#

public byte[] RenderReport(string reportName, string reportFormat) 
{ 
    LocalReport report = LoadReport(reportName); 

    //Has same name like DataSet in *.rdlc 
    ReportDataSource rds = new ReportDataSource("DataSet1", getData()); 

    report.DataSources.Clear(); 
    report.DataSources.Add(rds); 

    return report.Render(reportName); 
} 

private DataTable getData() 
{ 
    DataTable dt = new DataTable(); 

    dt.Columns.Add(new DataColumn("ID",typeof(System.String))); 
    dt.Columns.Add(new DataColumn("NAME", typeof(System.String))); 

    dt.Rows.Add(new string[] { "1", "Me" }); 
    return dt; 
} 

* .rdlc

<DataSources> 
    <DataSource Name="DataSource1"> 
     <ConnectionProperties> 
     <DataProvider>System.Data.DataSet</DataProvider> 
     <ConnectString>/* Local Connection */</ConnectString> 
     </ConnectionProperties> 
    </DataSource> 
    </DataSources> 
    <DataSets> 
    <DataSet Name="DataSet1"> 
     <Query> 
     <DataSourceName>DataSource1</DataSourceName> 
     <CommandText>/* Local Query */</CommandText> 
     </Query> 
     <Fields> 
     <Field Name="ID"> 
      <DataField>ID</DataField> 
      <rd:TypeName>System.String</rd:TypeName> 
     </Field> 
     <Field Name="NAME"> 
      <DataField>NAME</DataField> 
      <rd:TypeName>System.String</rd:TypeName> 
     </Field> 
     </Fields> 
    </DataSet> 
    </DataSets> 

但是,如果有一個數據源/數據集定義我刪除我獲得的數據源/數據集定義

{Microsoft.Reporting.DefinitionInvalidException: 報告的定義''無效。 ---> Microsoft.ReportingServices.ReportProcessing.ReportPublishingException: 文本框'Textbox1'的值表達式指的是字段 'ID'。報表項表達式只能引用 當前數據集範圍內的字段,或者如果在聚合內,則指定指定的 數據集作用域。在字段的名稱字母必須使用正確的 情況。}

難道我總是要創造這樣一個「虛擬」 -datasource/DataSet或我思念在我的代碼的東西嗎? 我希望在渲染過程之前有另一種解決方案來處理XML,有什麼想法?

謝謝!

回答

0

如果您正在使用RDLC並將RDLC嵌入到您的項目中,那麼您不能將RDLC留在DataSet中。 要麼你離開的DataSet固定的,只有改變它的項目均試圖從XML加載報表定義

// Valid XML with dynamic DataSources and DataSets 
string s = @"<?xml version=""1.0"" encoding=""utf-8""?><Report ...>...</Report>"; 
report.LoadReportDefinition(new MemoryStream(Encoding.UTF8.GetBytes(s))); 
return report.Render(reportName); 
+0

report.Render接受「字符串格式」作爲參數,而不是報告名稱。 – narayan

相關問題