Question

I have a webpage with gridview from Devexpress and i've implemented export to pdf. But somehow I don't get the current filter, order or group setting. I'm wondering if there is something wrong with how i set my setting in code or something since I've googled a lot at seems like those options should be handled automatically in ExportToPdf as long as you have rigth setting. My _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")

And finally 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();
        }
    }
}

Any clues?

Ps. Asking me why I don't write to DevExpress support is not helpful, so please just don't comment at all.

Was it helpful?

Solution

OTHER TIPS

After help for @Mikhail i fix the issue by putting @Html.Action("Grid","LogView") in the Html.BeginForm in _Log.View.cshtml like that:

<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")}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top