日期:2014-05-20  浏览次数:21164 次

GridView+联接水晶报表
各位大虾,请帮个忙,我做了一个GridView显示一个表1中的数据,通过表1中的关键字GCRCLMLH,作为选择,通过水晶报表显示出表2和表3中的部门数据.其中表2和表3和表1都是通过关键字GCRCLMLH作为主外键的.可是现在我点选择,总是提示我水晶报表生成失败,不知是什么原因.
HTML
<asp:GridView ID="GridView1" OnPageIndexChanging="GridView1_PageIndexChanging" 
  runat="server" AutoGenerateColumns="False" 
  width="100%" EmptyDataText="没有符合查询条件的数据!" AllowPaging="True" 
  DataKeyNames="GCRCLMLH" onpageindexchanged="GridView1_PageIndexChanged" 
  onselectedindexchanging="GridView1_SelectedIndexChanging" >
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
  <Report FileName="CG.rpt">
  <DataSources>
  <CR:DataSourceRef DataSourceID="SqlDataSource1" />
  </DataSources>
  <Parameters>
  <CR:ControlParameter ControlID="GridView1" ConvertEmptyStringToNull="False" 
  DefaultValue="" Name="newParameter1" PropertyName="SelectedValue" 
  ReportName="" />
  </Parameters>
  </Report>
  </CR:CrystalReportSource>



C#

 private void ConfigureCrystalReports()
  {
  ReportDocument myReport;
  String cn = @"Data Source=127.0.0.1;Initial Catalog=wcexxku;User ID=sa1;Password=sa";

  SqlDataAdapter sqlAdapt = new SqlDataAdapter();
  SqlConnection conn = new SqlConnection(cn);
  string sqlStr = "select HTXMB.[GCRCLMLH],CLB.*,CLCGB.* from HTXMB INNER JOIN CLB ON HTXMB.GCRCLMLH = CLB.GCRCLMLH INNER JOIN CLCGB ON CLB.GCRCLMLH=CLCGB.GCRCLMLH WHERE HTXMB.[GCRCLMLH]= " + GridView1.SelectedDataKey.Value.ToString();
  sqlAdapt = new SqlDataAdapter(sqlStr, conn);
  DataSet1 dt1 = new DataSet1();
  sqlAdapt.Fill(dt1, "myTable");

  myReport = new ReportDocument();
  myReport.Load(Server.MapPath("../CG.rpt"));

  myReport.SetDataSource(dt1);
  CrystalReportViewer1.ReportSource = myReport;}

------解决方案--------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
rel="stylesheet" type="text/css" />
<link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
rel="stylesheet" type="text/css" />
<link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>