C#报表控件reportview数据绑定

问题是这样的:首先通过查询得到数据显示在dataGridView上,然后通过打印将查询到数据数据显示在reportviewer上!第一次这样用,不知道怎么搞,希望大家能够... 问题是这样的:

首先通过查询得到数据显示在dataGridView上,然后通过打印将查询到数据数据显示在reportviewer上!

第一次这样用,不知道怎么搞,希望大家能够帮助我,不胜感激!
在回答问题的时候最好自己写个代码运行下,谢谢!然后把代码上传上来!
展开
 我来答
龍过鸡年
2013-11-04 · TA获得超过311个赞
知道小有建树答主
回答量:308
采纳率:100%
帮助的人:369万
展开全部

参考 Microsoft.Reporting.WinForms.ReportViewer:

http://msdn.microsoft.com/zh-cn/library/ms251671(v=vs.100).aspx

http://msdn.microsoft.com/zh-cn/library/microsoft.reporting.winforms.reportviewer(v=vs.100).aspx


WinForm 中 ReportViewer 需要 *.rdlc 文件作为报表的模板,这其实就是个 xml 文件。

文件主要定义了几个部分

1、DataSources

<DataSources>
    <DataSource Name="src">
        <ConnectionProperties>
            <DataProvider>System.Data.DataSet</DataProvider>
            <ConnectString></ConnectString>
        </ConnectionProperties>
    </DataSource>
</DataSources>

2、定义 DataProvider 中指定的内容,此处是 DataSet

<DataSets>
    <DataSet Name="rpt">
        <Fields>
            <Field Name="历史时间">
                <DataField>Date</DataField>
            </Field>
            <Field Name="输出频率">
                <DataField>Frequency</DataField>
            </Field>
            <Field Name="母线电压">
                <DataField>Voltage</DataField>
            </Field>
            <Field Name="输出电流">
                <DataField>Current</DataField>
            </Field>
        </Fields>
        <Query>
            <DataSourceName>src</DataSourceName>
            <CommandText>/* Local Query */</CommandText>
        </Query>
    </DataSet>
</DataSets>

3、声明报表的主体部分

<Body>
    <ReportItems>
        <Tablix Name="tab">
            <TablixBody>
                <TablixColumns>
                    <TablixColumn>
                        <Width>2in</Width>
                    </TablixColumn>
                    <TablixColumn>
                        <Width>2in</Width>
                    </TablixColumn>
                    <TablixColumn>
                        <Width>2in</Width>
                    </TablixColumn>
                    <TablixColumn>
                        <Width>2in</Width>
                    </TablixColumn>
                </TablixColumns>
                <TablixRows>
                    <TablixRow>
                        <Height>0.60cm</Height>
                        <TablixCells>
                            <TablixCell>
                                <CellContents>
                                    <Textbox Name="tbxDate">
                                        <CanGrow>true</CanGrow>
                                        <KeepTogether>true</KeepTogether>
                                        <Paragraphs>
                                            <Paragraph>
                                                <TextRuns>
                                                    <TextRun>
                                                        <Value>=Fields!Date.Value</Value>
                                                        <Style />
                                                    </TextRun>
                                                </TextRuns>
                                                <Style />
                                            </Paragraph>
                                        </Paragraphs>
                                        <Style>
                                            <Border />
                                            <PaddingLeft>2pt</PaddingLeft>
                                            <PaddingRight>2pt</PaddingRight>
                                            <PaddingTop>2pt</PaddingTop>
                                            <PaddingBottom>2pt</PaddingBottom>
                                        </Style>
                                    </Textbox>
                                </CellContents>
                            </TablixCell>
                            <TablixCell>
                                <CellContents>
                                    <Textbox Name="tbxFrequency">
                                        <CanGrow>true</CanGrow>
                                        <KeepTogether>true</KeepTogether>
                                        <Paragraphs>
                                            <Paragraph>
                                                <TextRuns>
                                                    <TextRun>
                                                        <Value>=Fields!Frequency.Value</Value>
                                                        <Style />
                                                    </TextRun>
                                                </TextRuns>
                                                <Style />
                                            </Paragraph>
                                        </Paragraphs>
                                        <Style>
                                            <Border />
                                            <PaddingLeft>2pt</PaddingLeft>
                                            <PaddingRight>2pt</PaddingRight>
                                            <PaddingTop>2pt</PaddingTop>
                                            <PaddingBottom>2pt</PaddingBottom>
                                        </Style>
                                    </Textbox>
                                </CellContents>
                            </TablixCell>
                            <TablixCell>
                                <CellContents>
                                <!-- ...... -->
                                </CellContents>
                            </TablixCell>
                            <TablixCell>
                                <CellContents>
                                <!-- ...... --/>
                                </CellContents>
                            </TablixCell>
                        </TablixCells>
                    </TablixRow>
                </TablixRows>
            </TablixBody>
            <TablixColumnHierarchy>
                <TablixMembers>
                    <TablixMember />
                    <TablixMember />
                    <TablixMember />
                    <TablixMember />
                </TablixMembers>
            </TablixColumnHierarchy>
            <TablixRowHierarchy>
                <TablixMembers>
                    <TablixMember>
                        <Group Name="colgrp" />
                    </TablixMember>
                </TablixMembers>
            </TablixRowHierarchy>
            <DataSetName>rpt</DataSetName>
            <Left>1.8cm</Left>
            <Height>0.61cm</Height>
            <Width>18cm</Width>
            <Style>
                <Border>
                    <Style>None</Style>
                </Border>
            </Style>
        </Tablix>
    </ReportItems>
    <Height>17.8cm</Height>
    <Style>
        <Border>
            <Style>None</Style>
        </Border>
    </Style>
</Body>

由于字数限制,部分内容就省略了,定义和上面的类似,改个名字就行了


4、最后是 Page 声明,包括页眉页脚等

<Page>
    <PageHeader>
        <Height>7.4cm</Height>
        <PrintOnFirstPage>true</PrintOnFirstPage>
        <PrintOnLastPage>true</PrintOnLastPage>
        <ReportItems>
            <Textbox Name="tbxTitle">
                <KeepTogether>true</KeepTogether>
                <Paragraphs>
                    <Paragraph>
                        <TextRuns>
                            <TextRun>
                                <Value>历史数据报表</Value>
                            </TextRun>
                            <Style />
                        </TextRuns>
                        <Style />
                    </Paragraph>
                </Paragraphs>
                <Top>2.7cm</Top>
                <Left>2.8cm</Left>
                <Height>0.61cm</Height>
                <Width>1cm</Width>
                <Style>
                    <Border>
                        <Style>None</Style>
                    </Border>
                    <PaddingLeft>2pt</PaddingLeft>
                    <PaddingRight>2pt</PaddingRight>
                    <PaddingTop>2pt</PaddingTop>
                    <PaddingBottom>2pt</PaddingBottom>
                </Style>
            </Textbox>
        </ReportItems>
        <Style>
            <Border>
                <Style>None</Style>
            </Border>
        </Style>
    </PageHeader>
    <PageFooter>
        <Height>4.2cm</Height>
        <PrintOnFirstPage>true</PrintOnFirstPage>
        <PrintOnLastPage>true</PrintOnLastPage>
        <Style>
            <Border>
                <Style>None</Style>
            </Border>
        </Style>
    </PageFooter>
    <PageHeight>29.41cm</PageHeight>
    <PageWidth>21.5cm</PageWidth>
    <InteractiveHeight>29.41cm</InteractiveHeight>
    <InteractiveWidth>21.5cm</InteractiveWidth>
    <LeftMargin>0cm</LeftMargin>
    <RightMargin>0cm</RightMargin>
    <TopMargin>0cm</TopMargin>
    <BottomMargin>0cm</BottomMargin>
    <ColumnSpacing>0.13cm</ColumnSpacing>
    <Style />
</Page>

这是以前的数据模板,临时修改的,可能会有错


更详细的信息请参考:

http://msdn.microsoft.com/en-us/library/dd297486.aspx

下载第三个条目,是 rdlc 文档的格式说明


调用:

var table = new System.Data.DataTable();
var rds = new Microsoft.Reporting.WinForms.ReportDataSource();
rds.Name = "rpt"; // 这里名称要和 rdlc 中的 DataSet 名称一致
rds.Value = table;

reportView1.LocalReport.DataSources.Add(rds);
reportView1.LocalReport.ReportEmbeddedResource = "historyrpt.xml";
reportView1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
reportView1.ZoomPercent = 100;
reportView1.RefreshReport();
更多追问追答
追问
兄弟能做个DEMO发过来吗?很着急,谢谢了!我的email804759109
追答
WPF WIndow 还是 WinForm
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式