这是一篇继 海波 发表“经典水晶报表--单击表头排序”于 @ 2003年12月25日 10:26:00 ) 的首篇C#版本。
若有人转载请注明出处。
本文同步发布于我的博客:《经典水晶报表--单击表头排序(2008年版)》
http://blog.csdn.net/flydragon0815/archive/2008/12/21/3568052.aspx
开发环境: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.aspx