水晶报表动态后台绑定代码

前台

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dkjybgDY.aspx.cs" Inherits="MK_dkjybg_dkjybgDY" %>

<%@ Register assembly="CrystalDecisions.Web, Version=10.5.3700.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 id="Head1" runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td align="center">
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="True" DisplayGroupTree="False"
EnableDatabaseLogonPrompt="False" HasExportButton="False"
HasToggleGroupTreeButton="False" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
</CR:CrystalReportSource>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;

public partial class MK_dkjybg_dkjybgDY : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DH"].ConnectionString);
DataSet ds = new DataSet();
sqlconn g_conn = new sqlconn();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Page_Init(object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN");

TableLogOnInfo info = new TableLogOnInfo();
TableLogOnInfos infos = new TableLogOnInfos();
ConnectionInfo conns = new ConnectionInfo();
conns.ServerName = ConfigurationManager.AppSettings["ip"].ToString();//数据库ip
conns.DatabaseName = ConfigurationManager.AppSettings["date"].ToString();//数据库名
conns.UserID = ConfigurationManager.AppSettings["use"].ToString();//数据库sa
conns.Password = ConfigurationManager.AppSettings["pass"].ToString();//数据库密码
info.ConnectionInfo = conns;
infos.Add(info);
this.CrystalReportViewer1.LogOnInfo = infos;

ReportDocument erp = new ReportDocument();
erp.Load(Server.MapPath("Cdkjybg.rpt"));

int NMId = int.Parse(Session["dkjybg"].ToString());
DataTable dt = g_conn.GetDataSet("select dkjybg_id 内码,dkjybg_djbm 单据编码 from dkjybg where dkjybg_id=" + NMId + "").Tables[0];
foreach (DataRow dr in dt.Rows)
{
TextObject txtdjbh = (TextObject)erp.ReportDefinition.ReportObjects["txtdjbh"];
txtdjbh.Text = dr["单据编码"].ToString();
}

ds.Clear();
string sql = string.Empty;

sql += "select * from dkjybgmx where dkjybgmx_btid=" + NMId + " order by dkjybgmx_xh";

SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter sqlad = new SqlDataAdapter();
sqlad.SelectCommand = com;
sqlad.Fill(ds);


erp.SetDataSource(ds.Tables[0]);


CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = erp;
CrystalReportViewer1.DataBind();

//直接打印
//CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 0, 0);
}
}

原文地址:https://www.cnblogs.com/txws1119/p/6004107.html