简体   繁体   English

使用实体框架在 Crystal Reports 中显示 GridView 的一条记录

[英]Show one record of GridView in Crystal Reports using Entity Framework

Scenario:设想:

I have a Gridview with some records which read from SQL Database, each record of Gridview has a button that use CommandName and CommandArgument with sending '<%#Eval("UserId")%>' to detecting each record with calling them in code behind (CS).我有一个带有一些从 SQL 数据库读取的记录的 Gridview,Gridview 的每条记录都有一个按钮,该按钮使用CommandNameCommandArgument并发送'<%#Eval("UserId")%>'来检测每条记录,并在后面的代码中调用它们(CS)。

Goal:目标:

I want to use entity command to when i click on the button ( DO_PRINT(LinkButton) ), the crystal report pop up showing content of just same record.我想使用实体命令,当我点击按钮( DO_PRINT(LinkButton) )时,水晶报告弹出,显示相同记录的内容。

Gridview like this:像这样的网格视图:

UserId    Name   LastName   OfficeId    Print
100       Hassan Hosseini      1        DO_PRINT(LinkButton)
200       Brad   Pitt          2        DO_PRINT(LinkButton)

Thank you in advance先感谢您

You just need to set the href of the link to a page hosting a crystal report viewer showing a report and in page load of the page, setup data source using the entity Id that you receive from query string or route.您只需要将链接的 href 设置为托管显示报告的水晶报表查看器的页面,并在页面的页面加载中,使用从查询字符串或路由接收的实体 Id 设置数据源。

Crystal Reports with Entity Framework - Pass data to report Crystal Reports with Entity Framework - 将数据传递给报告

  1. Create a ASP.NET WebForms Project创建 ASP.NET WebForms 项目
  2. Add a new Database to App_Data folder, name it SampleDatabase添加一个新的数据库到App_Data文件夹,命名为SampleDatabase
  3. Add a new table to the sample database, name it Products :向示例数据库添加一个新表,将其命名为Products

     CREATE TABLE [dbo].[Prodcts] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY(1,1), [Name] NVARCHAR(50) NOT NULL, [Price] INT NOT NULL, [Description] NVARCHAR(500) NULL )
  4. Add a few records to table.向表中添加一些记录。 (Otherwise the GridView will not be shown). (否则将不会显示 GridView)。

  5. Add a new entity data model to your project and name it SampleDatabase将新的实体数据模型添加到您的项目并将其命名为SampleDatabase
  6. Create a new page names Products.aspx and add a new GridView to the page using the following configs:创建一个名为Products.aspx的新页面,并使用以下配置向页面添加一个新的GridView

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name"/> <asp:BoundField DataField="Price" HeaderText="Price"/> <asp:BoundField DataField="Description" HeaderText="Description"/> <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="~\\Report.aspx?Id={0}" HeaderText="Report" Text="Report" /> </Columns> </asp:GridView>
  7. Load data into the grid, in the code behind:将数据加载到网格中,在后面的代码中:

     protected void Page_Load(object sender, EventArgs e) { using (var db = new SampleDatabaseEntities()) { var data = db.Prodcts.ToList(); GridView1.DataSource = data; GridView1.DataBind(); } }
  8. Add a new Crystal Report to the project, name it ProductReport :向项目中添加一个新的 Crystal Report,将其命名为ProductReport

    • Add New Item → Crystal Reports → Using the Report Wizard / Standard添加新项目 → Crystal Reports → 使用报表向导/标准
    • Then from Available Data Sources: → Project Data → .NET Objects → Find and choose the Product and add it to the right panel然后从可用数据源:→项目数据→.NET对象→查找并选择产品并将其添加到右侧面板
    • Then follow the wizard然后按照向导
  9. Add a new page named Report.aspx and drop an instance of the ReportViwer to the page, using the default name CrystalReportViewer1 .添加一个名为Report.aspx的新页面,并使用默认名称CrystalReportViewer1ReportViwer一个实例拖放到该页面。

  10. In the code behind of the Report.aspx , get the report and the data to show in report:Report.aspx后面的代码中,获取要在报告中显示的报告和数据:

     protected void Page_Load(object sender, EventArgs e) { if (int.TryParse(Request.QueryString["id"], out int id)) { using (var db = new SampleDatabaseEntities()) { var report = new ProductReport(); var data = db.Prodcts.Where(x => x.Id == id).ToList(); report.SetDataSource(data); CrystalReportViewer1.ReportSource = report; CrystalReportViewer1.RefreshReport(); } } }

Note: In case you had some script error preventing the report viewer from showing, you can copy C:\\inetpub\\wwwroot\\aspnet_client folder to your project.注意:如果您有一些脚本错误导致报告查看器无法显示,您可以将C:\\inetpub\\wwwroot\\aspnet_client文件夹复制到您的项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM