首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 實現水晶報表點擊列標排序完整代碼 (三):後台綁定與排序邏輯

     編輯:關於C語言

完整後台代碼


using System;
using System.Data;
using System.Configuration;
using System.Web.UI;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

public partial class _Default : Page
{
    string connStr = ConfigurationManager.ConnectionStrings["MyconnectionStrings"].ToString();
    static ReportDocument reportDoc = new ReportDocument();
    static string sortField = null;
    static bool asc = false;      // 排序方向切換旗標
    static bool postBackPaging = false; // 翻頁標志

    protected void Page_Load(object sender, EventArgs e) { }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (IsPostBack)
        {
            reportDoc = (ReportDocument)Session["Report"];
            if (reportDoc == null) ReportDoc_Bind();
            sortField = null;
            crystalReportViewer.ReportSource = reportDoc;
            postBackPaging = true;
        }
        if (postBackPaging) { postBackPaging = false; }
        else
        {
            if (reportDoc != null)
            {
                sortField = Request.QueryString["FieldName"];
                if (sortField != null) GetFieldName(sortField);
            }
        }
    }

    private void GetFieldName(string fieldName)
    {
        try
        {
            FieldDefinition fld = reportDoc.Database.Tables[0].Fields[fieldName];
            reportDoc.DataDefinition.SortFields[0].Field = fld;
            if (!asc)
            {
                reportDoc.DataDefinition.SortFields[0].SortDirection = SortDirection.AscendingOrder;
                asc = true;
            }
            else
            {
                reportDoc.DataDefinition.SortFields[0].SortDirection = SortDirection.DescendingOrder;
                asc = false;
            }
            crystalReportViewer.ReportSource = reportDoc;
            crystalReportViewer.ShowLastPage(); // 觸發頁數計算
            crystalReportViewer.ShowFirstPage();
        }
        catch (Exception ex)
        {
            Response.Write("<b><font color='#FF0000'>Operate Error!</font></b>");
        }
    }

    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";

        using (SqlConnection conn = new SqlConnection(connStr))
        using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
        {
            DataSet ds = new DataSet();
            da.Fill(ds);
            ReportDocument doc = new ReportDocument();
            doc.Load(Server.MapPath("Crystalrpt.rpt"));
            doc.SetDataSource(ds.Tables[0]);
            Session["Report"] = doc;
            reportDoc = doc;
        }
    }

    protected void btnOk_Click(object sender, EventArgs e) { ReportDoc_Bind(); crystalReportViewer.ReportSource = reportDoc; }
}

關鍵技術點

  • OnPreRender 區分 PostBack 與超連結排序請求。
  • Session 快取 ReportDocument 避免重複查詢。
  • 報表設計時需預先在「記錄排序專家」加入一個排序欄位佔位。
  • 超連結 URL 格式:Page.aspx?FieldName=欄位名。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved