Use custom objects as the source for Microsoft Rep

2020-04-16 01:50发布

问题:

In some instances, I prefer working with custom objects instead of strongly typed datasets and data rows. However, it seems like Microsoft Reporting (included with VS2005) requires strongly typed datasets.

Is there a way to use my custom objects to design and populate reports?

回答1:

I found the answer. Yes, it's possible. You just have to add a custom object as a datasource in visual studio.

http://www.gotreportviewer.com/objectdatasources/index.html



回答2:

I could never choose one of my own POCOs in Report Data setup from my project to be a model for the report - the alleged 'global' option mentioned in the walkthrough was not there. So I ended up having to edit the XML to define the type and an imitation data source (which does not actually exist in my project).

I assign the data of type Aies.Core.Model.Invoice.MemberInvoice to the report in code

reportViewer.LocalReport.DataSources.Add(new ReportDataSource("MemberInvoice", new[] { invoice1 }));

And the custom definition is:

  <DataSources>
    <DataSource Name="MemberInvoice">
      <ConnectionProperties>
        <DataProvider>System.Data.DataSet</DataProvider>
        <ConnectString>/* Local Connection */</ConnectString>
      </ConnectionProperties>
      <rd:DataSourceID>3fe04def-105a-4e9b-99db-630c1f8bb2c9</rd:DataSourceID>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="MemberInvoice">
      <Fields>
        <Field Name="MemberId">
          <DataField>MemberId</DataField>
          <rd:TypeName>System.Int32</rd:TypeName>
        </Field>
        <Field Name="DateOfIssue">
          <DataField>DateOfIssue</DataField>
          <rd:TypeName>System.DateTime</rd:TypeName>
        </Field>
        <Field Name="DateDue">
          <DataField>DateDue</DataField>
          <rd:TypeName>System.DateTime</rd:TypeName>
        </Field>
        <Field Name="Amount">
          <DataField>Amount</DataField>
          <rd:TypeName>System.Decimal</rd:TypeName>
        </Field>
      </Fields>
      <Query>
        <DataSourceName>MemberInvoice</DataSourceName>
        <CommandText>/* Local Query */</CommandText>
      </Query>
      <rd:DataSetInfo>
        <rd:DataSetName>Aies.Core.Model.Invoice</rd:DataSetName>
        <rd:TableName>MemberInvoiceData</rd:TableName>
        <rd:ObjectDataSourceSelectMethod>GetInvoices</rd:ObjectDataSourceSelectMethod>
        <rd:ObjectDataSourceSelectMethodSignature>System.Collections.Generic.IEnumerable`1[Aies.Core.Model.Invoice.MemberInvoice] GetInvoices()</rd:ObjectDataSourceSelectMethodSignature>
        <rd:ObjectDataSourceType>Aies.Core.Model.Invoice.MemberInvoiceData, Aies.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</rd:ObjectDataSourceType>
      </rd:DataSetInfo>
    </DataSet>
  </DataSets>


回答3:

I believe you can set up SSRS to read data values from a more or less arbitrary object. This Link describes the IDataReaderFieldProperties object in the API which (IIRC) allows you to specify the getter method to invoke to get a value.