简体   繁体   English

ASP.NET中的Telerik报表-以编程方式应用过滤器

[英]Telerik Report in Asp.Net-Apply Filters in programatically

My Requirement: Apply fillers via c# coding(Not Design) ie, filterer salaries greater than 7000. 我的要求:通过c#编码(非设计)应用填充器,即过滤器的工资大于7000。

I have a class library and a web form in my project. 我的项目中有一个类库和一个Web表单。 I am creating the report on class library and display report by using web form. 我在类库上创建报告,并使用Web表单显示报告。

When I run my application it shows always the unfiltered data. 当我运行我的应用程序时,它总是显示未过滤的数据。 What i do to get Filtered data in Viewer. 我要如何在Viewer中获取过滤数据。

Code: 码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Telerik.Reporting.Filter f1 = new Telerik.Reporting.Filter();
        f1.Expression = "= Fields.Salary";
        f1.Operator = Telerik.Reporting.FilterOperator.GreaterOrEqual;
        f1.Value = "=7000";
        EmpReport objEmpReport = new EmpReport(); objEmpReport.Filters.Add(f1);
        TypeReportSource rptSource = new TypeReportSource(); rptSource.TypeName = typeof(EmpReport).AssemblyQualifiedName; this.ReportViewer1.ReportSource = rptSource;
    }
}

working code: 工作代码:

// ...
using Telerik.Reporting;
using Telerik.Reporting.Processing;
// ...

void ExportToPDF(string reportToExport)
{
    // all my reports are in trdx format - detect file type and use unpackage below for trdp files.
    string currPath = HttpRuntime.AppDomainAppPath;                 // get the full path so deserialise works
    reportToExport = currPath + @"Reports\" + reportToExport;       // add folder and report name to path
    UriReportSource uriReportSource = new UriReportSource { Uri = reportToExport };   // compressed to 1 line for brevity
    Telerik.Reporting.Report myReport = DeserializeReport(uriReportSource);    // extract report from xml format (trdx)

    // Filters are client side (use params for server side) Here we work with the report object.
    // set meaningful field name and values for your code, maybe even pass in as params to this function...
    myReport.Filters.Add("UserId", FilterOperator.Equal , "1231");

    var instanceReportSource = new Telerik.Reporting.InstanceReportSource();    // report source
    instanceReportSource.ReportDocument = myReport;         // Assigning Report object to the InstanceReportSource

    // kinda optional, lots of examples just used null instead for deviceInfo
    System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();   // set any deviceInfo settings if necessary

    ReportProcessor reportProcessor = new ReportProcessor();                        // will do work
    RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo);   // GO!

    if (!result.HasErrors)
    {
        this.Response.Clear();
        this.Response.ContentType = result.MimeType;
        this.Response.Cache.SetCacheability(HttpCacheability.Private);
        this.Response.Expires = -1;
        this.Response.Buffer = true;
        this.Response.BinaryWrite(result.DocumentBytes);
        this.Response.End();
    }
    else
    {
        Exception[] ex = result.Errors;
        throw new Exception(ex[0].Message);
    }
}

Telerik.Reporting.Report DeserializeReport(UriReportSource uriReportSource)
{
    var settings = new System.Xml.XmlReaderSettings();
    settings.IgnoreWhitespace = true;
    using (var xmlReader = System.Xml.XmlReader.Create(uriReportSource.Uri, settings))
    {
        var xmlSerializer = new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();
        var report = (Telerik.Reporting.Report)xmlSerializer.Deserialize(xmlReader);
        return report;
    }
}

Telerik.Reporting.Report UnpackageReport(UriReportSource uriReportSource)
{
    var reportPackager = new ReportPackager();
    using (var sourceStream = System.IO.File.OpenRead(uriReportSource.Uri))
    {
        var report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
        return report;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM