序:
這是一篇繼 海波發表“經典水晶報表--單擊表頭排序”於 @ 2003年12月25 日 10:26:00 )的首篇C#版本。
若有人轉載請注明出處。
本文同步發布於我的博客:《經典水晶報表--單擊表頭排序(2008年版)》
http://blog.csdn.net/flydragon0815/archive/2008/12/21/3568052.aspx< /P>
開發環境:VS2005,及該版本自帶的水晶報表
本文的前提是,報表都能完全顯示數據
1.單擊表頭的第一個字段:右鍵→設置對象格式,如下圖
2.然後在“格式編輯器”中,超級鏈接→Internet上的網站(A),然後在“網 站地址(I)”下面輸入:“Flydragon0815.aspx?FieldName=Location”,本頁面 的名字:“Flydragon0815.aspx”,“FieldName”為參數的名字,也就是該字 段的名稱,“Flydragon0815.aspx”為要加載該報表的頁面。
“Flydragon0815.aspx?FieldName=Location”這樣寫的目的是把“Location ”這個名字傳到的後台代碼中,以便控制排序
3.這一步比較重要,在報表的空白處右鍵:報表→記錄排序專家
4 把該字添加到“排序字段”中,提示:這一步較重要,其它字 段排序時不用選了,選擇這一個就可以了,如果多選的話,可能就不能排序了。
這些基本的操作完成後,先看一下預覽的界面,按Loaction排序前
按“Location”排序後:
以下是源代碼:
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;//顯示報表
}
}
=================================================== ================
轉載請注明出處:
http://blog.csdn.net/flydragon0815/archive/2008/12/21/3568052.a spx