以下是源代碼:
C# code
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;//顯示報表
}
}
=================================================== ================