4,816
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
public partial class _Default : System.Web.UI.Page
{
public string g_MyConnection = System.Configuration.ConfigurationManager.ConnectionStrings["MyconnectionStrings"].ToString();
private static ReportDocument m_ReportDoc =new ReportDocument();
private static string m_strGetFldName = null;
private static bool m_FlagOrder = false;//用来判断排序的方式:AscendingOrder,DescendingOrder
private static bool m_FlagOverPage = false;//控制是否是“IsPostBack”的翻页标志
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (IsPostBack)
{
//获取缓存的报表
m_ReportDoc = (ReportDocument)Session["Report"];
if (m_ReportDoc == null)
{
ReportDoc_Bind();
}
m_strGetFldName = null;
this.crystalReportViewer.ReportSource = m_ReportDoc;
m_FlagOverPage = true;
}
if (m_FlagOverPage)
{
m_FlagOverPage = false;
}
else
{
if (m_ReportDoc != null)
{
m_strGetFldName = Request.QueryString["FieldName"];
if (m_strGetFldName != null)
{
GetFieldName(m_strGetFldName);
}
}
}
}
/// <summary>
/// 获得要排序的字段
/// </summary>
/// <param name="strFieldName"></param>
private void GetFieldName(string strFieldName)
{
try
{
FieldDefinition FieldDef = null;
FieldDef = m_ReportDoc.Database.Tables[0].Fields[strFieldName];//把从“数据库”中取的该字段赋给 FieldDef
m_ReportDoc.DataDefinition.SortFields[0].Field = FieldDef;//然后再赋给要排序的字段名称
if (m_FlagOrder == false)
{
m_ReportDoc.DataDefinition.SortFields[0].SortDirection = CrystalDecisions.Shared.SortDirection.AscendingOrder;
m_FlagOrder = true;
}
else
{
m_ReportDoc.DataDefinition.SortFields[0].SortDirection = CrystalDecisions.Shared.SortDirection.DescendingOrder;
m_FlagOrder = false;
}
crystalReportViewer.ReportSource = m_ReportDoc;
crystalReportViewer.ShowLastPage(); //获取页数
crystalReportViewer.ShowFirstPage();
}
catch (Exception ee)
{
Response.Write("<b><font color='#FF0000'>Operate Error!</font></b>");
}
}
/// <summary>
/// 绑定报表
/// </summary>
private void ReportDoc_Bind()
{
string sql = "select ct.XK_TEXT as Location, pt.XK_RECEIPT_DATE_TIME as DateStart, " +
" pt.XK_RECEIPT_DATE_TIME as DateEnd, pt.XK_PLU as Dept, pt.XK_RECEIPT_NUM as Receipt#, " +
" p.XK_TEXT as Item#, pt.XK_QUANTITY as Quantity, pt.XK_AMOUNT as Amount " +
" from XK_CASH_TERMINAL ct left outer join XK_POS_TRANSACTION pt on ct.XK_ID = pt.XK_CASH_TERMINAL_ID " +
" left outer join XK_PRODUCT p on pt.XK_PROD_CODE = p.XK_CODE where 1=1 and pt.XK_STATUS IS NULL ";
SqlConnection conn = new SqlConnection(g_MyConnection);
System.Data.SqlClient.SqlDataAdapter MyCommand = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
MyCommand.Fill(ds);
ReportDocument m_ReportDoc = new ReportDocument();
m_ReportDoc.Load(Server.MapPath("Crystalrpt.rpt"));
m_ReportDoc.SetDataSource(ds.Tables[0]);
Session["Report"] = m_ReportDoc;
conn.Close();
}
protected void btnOk_Click(object sender, EventArgs e)
{
ReportDoc_Bind();
this.crystalReportViewer.ReportSource = m_ReportDoc;//显示报表
}
}