DevExpress GridView ExportToPdf不会导出过滤后的数据MVC

时间:2014-04-24 13:57:12

标签: asp.net-mvc gridview devexpress export

我有一个来自Devexpress的gridview的网页,我已经实现了导出为pdf。但不知何故,我没有得到当前的过滤器,订单或组设置。我想知道如果我在代码中设置我的设置有什么问题,因为我已经google了很多,看起来这些选项应该在ExportToPdf中自动处理,只要你有设置。我的_GripPartial.cshtml:

@Html.DevExpress().GridView(TreeMenuTest.Controllers.LogViewController.GridViewSettings).Bind(TreeMenuTest.Controllers.LogViewController.DataSource).GetHtml()

_LogView.cshtml:

    @using (Html.BeginForm("ExportToPDF", "LogView", FormMethod.Post))
{
 <div id="buttonExport" class ="gridBtn">
             <input type="submit" id ="ExportBtn" value="Export to pdf"  />
</div>}
<div id="buttonReset" class ="gridBtn">
             <input type="button" id ="ResetBtn" value="Reset Grid" onclick="javascript: ResetGrid()"/>
</div>
@Html.Action("Grid","LogView")

最后是LogViewController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using TreeMenuTest.Models;
using DevExpress.Web.Mvc;
using DevExpress.Web.ASPxGridView;
using System.Globalization;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrintingLinks;
using System.IO;
using System.Drawing.Printing;
namespace TreeMenuTest.Controllers
{
    public class LogViewController : Controller
    {
        //
        // GET: /LogView/
        public static List<LoggingEvent> DataSource;
        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings GridViewSettings
        {
            get
            {
                if (exportGridViewSettings == null)
                    exportGridViewSettings = GetGridViewSettings();
                return exportGridViewSettings;
            }
        }
        static GridViewSettings GetGridViewSettings()
        {
            GridViewSettings settings = new GridViewSettings();
            settings.Name = "GridView";
            settings.CallbackRouteValues = new { Controller = "LogView", Action = "Grid" };
            settings.Width = Unit.Percentage(100);
            settings.Theme = "BlackGlass";
            settings.KeyFieldName = "Id";
            settings.SettingsPager.Visible = true;
            settings.Settings.ShowGroupPanel = true;
            settings.Settings.ShowFilterRow = true;
            settings.SettingsBehavior.AllowSelectByRowClick = true;
            settings.SettingsPager.PageSize = 25;
            settings.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.Control;
            settings.Settings.ShowHeaderFilterButton = true;
            settings.SettingsPopup.HeaderFilter.Height = 200;
            settings.SettingsExport.Landscape = true;
            settings.SettingsExport.TopMargin = 0;
            settings.SettingsExport.LeftMargin = 0;
            settings.SettingsExport.RightMargin = 0;
            settings.SettingsExport.BottomMargin = 0;
            settings.SettingsExport.PaperKind = PaperKind.A4;
            settings.SettingsExport.RenderBrick = (sender, e) =>
            {
                if (e.RowType == GridViewRowType.Data && e.VisibleIndex % 2 == 0)
                    e.BrickStyle.BackColor = System.Drawing.Color.FromArgb(0xEE, 0xEE, 0xEE);
            };

            settings.Columns.Add("Id");
            settings.Columns.Add(column =>
            {
                column.FieldName = "Ts";
                column.Settings.AutoFilterCondition = AutoFilterCondition.Like;
            });
            settings.Columns.Add("LogLevelText").Caption = "Level";
            settings.Columns.Add("LoggerName");
            settings.Columns.Add("Message");
            settings.Columns.Add("ThreadName").Caption = "Thread";
            settings.Columns.Add("UserName");
            settings.Columns.Add("Domain");
            settings.Columns.Add("ClassName");
            settings.Columns.Add("FileName");
            settings.Columns.Add("MethodName").Caption = "Method";
            settings.Columns.Add("LineNumber").Caption = "Line";
            settings.Columns.Add("LocalIP");
            settings.Columns.Add("MachineName").Caption = "Machine";
            settings.Columns.Add("UnikeName");

            settings.Settings.ShowPreview = true;
            settings.PreviewFieldName = "ExceptionString";
            return settings;
        }
        public ActionResult Index()
        {
            return PartialView("_LogView");
        }
        public ActionResult Grid()
        {
            if (DataSource == null)
            {
                DataSource = GetAllEventsLast24h();
            }
            return PartialView("_GridPartial");
        }
        public ActionResult EmptyGrid()
        {
            DataSource = null;
            return PartialView("_GridPartial");
        }
        public List<LoggingEvent> GetAllEventsLast24h() {
            return GetEventsWithCommand("where Ts > '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss") + "';");
        }
        public List<LoggingEvent> GetEventsOnDateTime(DateTime fromDate, DateTime toDate)
        {
            return GetEventsWithCommand("where Ts > '" + fromDate.ToString("yyyy-MM-dd HH:mm:ss") + "' AND Ts < '" + toDate.ToString("yyyy-MM-dd HH:mm:ss") + "';");  
        }
        public List<LoggingEvent> GetEventsWithCommand(string where)
        {
            List<LoggingEvent> events = new List<LoggingEvent>();
            SqlConnection myConnection = new SqlConnection("Data Source=xxx;user id=xxx;password=xxx;connection timeout=30"); 
            myConnection.Open();
            SqlDataReader myReader = null;
            SqlCommand command = new SqlCommand("SELECT Id,Ts,LogLevel,LoggerName,Message,ThreadName,UserName,ExceptionString,Domain,ClassName,FileName,MethodName,LineNumber,LocalIP,MachinName,UnikeName from dbo.LoggingEvent " + where);
            command.Connection = myConnection;
            myReader = command.ExecuteReader();
            while (myReader.Read())
            {
                events.Add(LoggingEvent.ReadEntityFromDbReader(myReader));
            }
            myConnection.Close();
            return events;
        }
        [HttpPost]
        public ActionResult ReloadGrid(string fromDate, string toDate)
        {
            DateTime fromDateTime;
            DateTime toDateTime;
            if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate)) // todo 
            {
                fromDateTime = ParseStringToDate(fromDate);
                toDateTime = ParseStringToDate(toDate);
            }
            else// No dates specified = get last 24 hours
            {
                toDateTime = DateTime.Now;
                fromDateTime = toDateTime.AddHours(-24);
            }
            if (fromDateTime.CompareTo(toDateTime) > 0)
            {
                // from date grater then todate, change places
                DateTime temp = toDateTime;
                toDateTime = fromDateTime;
                fromDateTime = temp;
            }
            DataSource = GetEventsOnDateTime(fromDateTime, toDateTime);
            return PartialView("_LogView");
        }
        public DateTime ParseStringToDate(string datetxt)// todo this is copy froim treemenuviewcontroller, create utilsclass
        {
            const string dateformat = "dd.MM.yyyy HH:mm"; // jquery datepicker
            const string dateformat2 = "yyyy-MM-dd HH:mm";// chrome TODO different formats with different location setting?
            DateTime dateTime;
            try //todo error handling!
            {
                dateTime = DateTime.ParseExact(datetxt, dateformat, CultureInfo.InvariantCulture);
            }
            catch
            {
                dateTime = DateTime.ParseExact(datetxt, dateformat2, CultureInfo.InvariantCulture);
            }
            return dateTime;
        }
        public ActionResult ExportToPDF()
        {
            var printable = GridViewExtension.CreatePrintableObject(GridViewSettings, DataSource);

            PrintingSystem ps = new PrintingSystem();


            PrintableComponentLink link1 = new PrintableComponentLink(ps);
            link1.Component = printable;

            link1.PrintingSystem.Document.AutoFitToPagesWidth = 1;
            link1.Landscape = true;
            CompositeLink compositeLink = new CompositeLink(ps);
            compositeLink.Links.Add(link1);


            compositeLink.CreateDocument();
            using (MemoryStream stream = new MemoryStream())
            {
                compositeLink.PrintingSystem.ExportToPdf(stream);
                WriteToResponse("filename", true, "pdf", stream);
            }
            ps.Dispose();

            return Index();

        }
        void WriteToResponse(string fileName, bool saveAsFile, string fileFormat, MemoryStream stream)
        {
            string disposition = saveAsFile ? "attachment" : "inline";
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat));
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition",
            string.Format("{0}; filename={1}.{2}", disposition, fileName, fileFormat));
            Response.BinaryWrite(stream.GetBuffer());
            Response.End();
        }
    }
}

任何线索?

聚苯乙烯。问我为什么不写DevExpress支持没有帮助,所以请不要发表评论。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

在@Mikhail的帮助之后我通过将@ Html.Action(&#34; Grid&#34;,#34; LogView&#34;)放在_Log.View.cshtml中的Html.BeginForm中解决了这个问题:

<div id="buttonReset" class ="gridBtn">
             <input type="button" id ="ResetBtn" value="Reset Grid" onclick="javascript: ResetGrid()"/>
</div>
@using (Html.BeginForm("ExportToPDF", "LogView", FormMethod.Post))
{
 <div id="buttonExport" class ="gridBtn">
             <input type="submit" id ="ExportBtn" value="Export to pdf"  />
</div>

@Html.Action("Grid","LogView")}