Asp.Net中的Telerik报告 - 以编程方式应用过滤器

问题描述 投票:2回答:1

我的要求:通过c#编码(非设计)申请填充,即过滤器工资大于7000。

我的项目中有一个类库和一个Web表单。我正在使用Web表单创建关于类库和显示报告的报告。

当我运行我的应用程序时,它始终显示未过滤的数据。我如何在Viewer中获取过滤数据。

码:

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;
    }
}
c# asp.net filter telerik
1个回答
0
投票

工作代码:

// ...
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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.