Tuesday, March 27, 2012

Bind object to RDLC Report

Hi,all
I hava a question about Bind object to RDLC Report,the RDLC like
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSources>
<DataSource Name="DummyDataSource">
<ConnectionProperties>
<ConnectString />
<DataProvider>SQL</DataProvider>
</ConnectionProperties>
<rd:DataSourceID>47739726-cc8c-4719-b061-c392c2cceb68</rd:DataSourceID>
</DataSource>
</DataSources>
<BottomMargin>2.5cm</BottomMargin>
<RightMargin>2.5cm</RightMargin>
<PageWidth>21cm</PageWidth>
<rd:DrawGrid>true</rd:DrawGrid>
<InteractiveWidth>21cm</InteractiveWidth>
<rd:GridSpacing>0.25cm</rd:GridSpacing>
<rd:SnapToGrid>true</rd:SnapToGrid>
<Body>
<ColumnSpacing>1cm</ColumnSpacing>
<ReportItems>
<Table Name="table1">
<Footer>
<TableRows>
<TableRow>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="textbox7">
<rd:DefaultName>textbox7</rd:DefaultName>
<ZIndex>7</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox8">
<rd:DefaultName>textbox8</rd:DefaultName>
<ZIndex>6</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox6">
<rd:DefaultName>textbox6</rd:DefaultName>
<ZIndex>5</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox9">
<rd:DefaultName>textbox9</rd:DefaultName>
<ZIndex>4</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
<Height>0.63492cm</Height>
</TableRow>
</TableRows>
</Footer>
<DataSetName>WindowsApplication1_MedcialCase</DataSetName>
<Top>0.25cm</Top>
<Details>
<TableRows>
<TableRow>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="ID">
<rd:DefaultName>ID</rd:DefaultName>
<ZIndex>3</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>=Fields!ID.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="Name">
<rd:DefaultName>Name</rd:DefaultName>
<ZIndex>2</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>=Fields!PatientOfMedcialCase.Value.Name.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="Name_1">
<rd:DefaultName>Name_1</rd:DefaultName>
<ZIndex>1</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>=Fields!Name.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="PatientOfMedcialCase">
<rd:DefaultName>PatientOfMedcialCase</rd:DefaultName>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>=Fields!PatientOfMedcialCase.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
<Height>0.63492cm</Height>
</TableRow>
</TableRows>
</Details>
<Header>
<TableRows>
<TableRow>
<TableCells>
<TableCell>
<ReportItems>
<Textbox Name="textbox1">
<rd:DefaultName>textbox1</rd:DefaultName>
<ZIndex>11</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>ID</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox2">
<rd:DefaultName>textbox2</rd:DefaultName>
<ZIndex>10</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>Name</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox4">
<rd:DefaultName>textbox4</rd:DefaultName>
<ZIndex>9</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>Name</Value>
</Textbox>
</ReportItems>
</TableCell>
<TableCell>
<ReportItems>
<Textbox Name="textbox3">
<rd:DefaultName>textbox3</rd:DefaultName>
<ZIndex>8</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>Patient Of Medcial Case</Value>
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
<Height>0.63492cm</Height>
</TableRow>
</TableRows>
</Header>
<TableColumns>
<TableColumn>
<Width>5.33333cm</Width>
</TableColumn>
<TableColumn>
<Width>8cm</Width>
</TableColumn>
<TableColumn>
<Width>5.33333cm</Width>
</TableColumn>
<TableColumn>
<Width>5.33333cm</Width>
</TableColumn>
</TableColumns>
<Height>1.90476cm</Height>
</Table>
</ReportItems>
<Height>5cm</Height>
</Body>
<rd:ReportID>a69b2d8e-258a-4212-8d4a-c67c96055732</rd:ReportID>
<LeftMargin>2.5cm</LeftMargin>
<DataSets>
<DataSet Name="WindowsApplication1_MedcialCase">
<rd:DataSetInfo>
<rd:DataSetName>WindowsApplication1</rd:DataSetName>
<rd:ObjectDataSourceType>WindowsApplication1.MedcialCase, WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</rd:ObjectDataSourceType>
<rd:TableName>MedcialCase</rd:TableName>
</rd:DataSetInfo>
<Query>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
<CommandText />
<DataSourceName>DummyDataSource</DataSourceName>
</Query>
<Fields>
<Field Name="ID">
<rd:TypeName>System.Guid</rd:TypeName>
<DataField>ID</DataField>
</Field>
<Field Name="PatientOfMedcialCase">
<rd:TypeName>WindowsApplication1.Patient</rd:TypeName>
<DataField>PatientOfMedcialCase</DataField>
</Field>
<Field Name="Name">
<rd:TypeName>System.String</rd:TypeName>
<DataField>Name</DataField>
</Field>
</Fields>
</DataSet>
</DataSets>
<Width>24cm</Width>
<InteractiveHeight>29.7cm</InteractiveHeight>
<Language>en-US</Language>
<TopMargin>2.5cm</TopMargin>
<PageHeight>29.7cm</PageHeight>
</Report>

the object I bind to the Report like

public class MedcialCase
{
private Guid iD;

public Guid ID
{
get { return iD; }
set { iD = value; }
}

private Patient patient;

public Patient PatientOfMedcialCase
{
get { return patient; }
set { patient = value; }
}

}

public class Patient
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

}

and the code bind object to report like

MedcialCase medcialCase = new MedcialCase();
medcialCase.ID = Guid.NewGuid();
Patient patient = new Patient();
patient.Name = "Michael";
medcialCase.PatientOfMedcialCase = patient;
this.MedcialCaseBindingSource.DataSource = medcialCase;
this.reportViewer1.RefreshReport();

I choose the MedcialCase.ID and MedcialCase.PatientOfMedcialCase.Name to show, but when the report was run ,,just MedcialCase.ID has been shown,
the text of MedcialCase.PatientOfMedcialCase.Name is "=Fields!Name.Value"

I modify it to "=Fields!PatientOfMedcialCase.Value.Name.Value" and run it ,the "#Error" show in the text of Name

anyone can tell me why?and how to bind object like MedcialCase to the Report?

thanks!

This is snippet from

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

Nested objects

Only the first level of public properties can be dragged and dropped to a report. If the type of a property is a custom class with its own properties then you can manually enter an expression to access the values of second level properties. For example if the Employee class has a HomeAddress property of type Address then you can enter the expression =Fields!HomeAddress.Value.Zip to access the value of Zip property.

If the value of a property is a collection then you can use a subreport to display the collection. See this example.

In your case, this translates to "=Fields!PatientOfMedcialCase.Value.Patient.Name"

Hope that helps,

Tudor Trufinescu

|||

hi,Tudor

Thanks for your advice,

when I try to do follow your advice,I Modify the class like:

public class Book
{
private string m_title;
private string m_publisher;
private float m_listPrice;

public Book(string title, string publisher, float listPrice)
{
m_title = title;
m_publisher = publisher;
m_listPrice = listPrice;
}

public string Title
{
get { return m_title; }
set { m_title = value; }
}

public string Publisher
{
get { return m_publisher; }
set { m_publisher = value; }
}

public float ListPrice
{
get { return m_listPrice; }
set { m_listPrice = value; }
}
}

class Author
{
private int m_id;
private string m_name;
private Book book;

public Author(int id)
{
m_id = id;
}

public Author(string name, int id, Book books)
{
m_name = name;
m_id = id;
book = books;
}

public int Id
{
get { return m_id; }
set { m_id = value; }
}

public string Name
{
get { return m_name; }
set { m_name = value; }
}

public Book Books
{
get { return this.book; }
set { this.book = value; }
}
}

and report text like "=Fields!Books.Value.ListPrice" as you say,

when the report run ,the text still display the "#error"

How shall I do?

|||

I have changed the object data source sample to illustrate the use of nested objects

See this post for more details.

http://blogs.msdn.com/tudortr/archive/2006/04/10/NestedObjectsDataSource.aspx

Thanks,

Tudor

|||that's helpful,i have solved the problem,thanks!|||Just wondering why I get the following error when I use GenericsCollection:

"The Value expression for the textbox ‘AddressLine1’ refers to the field ‘Contacts’. Report item expressions can only refer to fields within the current data set scope or, if inside an aggregate, the specified data set scope. "

for example I have these attributes of a class Company:

public Address PrivateAddress
{
get { return _privateAddress; }
set
{
_privateAddress = value;
}
}

public GenericCollection<Address> Contacts
{
get { return _contacts; }
set
{
_contacts = value;
}
}

I have no problems using the PrivateAddress object, but the Contacts gives me the above error message.

Has anyone implemented Generics objects? Any help would be appreciated.
Thanks
sql

No comments:

Post a Comment