This is a migrated thread and some comments may be shown as answers.

Problem with exporting from filtered grid

3 Answers 25 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jakob
Top achievements
Rank 1
Jakob asked on 20 Sep 2011, 04:15 PM
Hi,

I'm having problems exporting from a filtered gridcontrol. Whenever I apply a filter, it is applied perfectly on the webinterface, but exporting to .pdf format seems to ignore the filter, resulting in a full list of the datasource in the pdf file.

I've spent half a day reading through your forums, and tried every relevant course, without any luck. I've noticed that most of these, are not using OnNeedDataSource as datasource, could my problem be here?

I would be happy to supply code snippets, but as I have tried at least 10 different things with no success, I don't think my problem lies in the export itself.

I am not doing anything custom with the filtering, but it seems like the MasterTableView has forgotten its filtering, once the pdf event is fired. Should I be doing anything to ensure that the filter persists?

Any and all help would be much appreciated - and if you need additional info I'll supply it ASAP :)

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Sep 2011, 08:13 PM
Hello,

please check below code snippet.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx.cs" Inherits="TelerikTest.Web.WebForm12" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
            AllowFilteringByColumn="true" onitemcommand="RadGrid1_ItemCommand">
            <MasterTableView CommandItemDisplay="Top">
            <CommandItemSettings ShowExportToPdfButton="true" />
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace TelerikTest.Web
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGrid();
            }
        }
 
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.FilterCommandName)
            {
                bindGrid();
            }
            if (e.CommandName == RadGrid.ExportToPdfCommandName)
            {
                bindGrid();
            }
        }
 
        protected void bindGrid()
        {
            dynamic data = new[] {
                new { ID = 1, Name ="Name1"},
                new { ID = 2, Name = "Name2"},
                new { ID = 3, Name = "Name3"},
                new { ID = 4, Name = "Name4"},
                new { ID = 5, Name = "Name5"}
            };
 
            RadGrid1.DataSource = data;
            RadGrid1.DataBind();
        }
    }
}

this code works for me let me its working or not in your side.

Thanks,
Jayesh Goyani
0
Jakob
Top achievements
Rank 1
answered on 21 Sep 2011, 09:29 AM
I can't seem to get this working.... When I try to filter my data now, the filter doesn't get applied at all, I just get the same result as on page_load.

Don't know what else to say, but here is my code:
Page
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<!-- content start -->
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Black" />
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1"
    ClientEvents-OnRequestStart="requestStart">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<div style="margin-left: 20px;">
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" OnItemCommand="RadGrid1_ItemCommand"
        AllowFilteringByColumn="True" AutoGenerateColumns="False">
        <GroupingSettings CaseSensitive="False" />
        <ExportSettings IgnorePaging="True" OpenInNewWindow="True">
            <Pdf PageHeight="210mm" PageWidth="297mm" DefaultFontFamily="Arial Unicode MS" PageBottomMargin="20mm"
                PageTopMargin="20mm" PageLeftMargin="20mm" PageRightMargin="20mm" />
        </ExportSettings>
        <MasterTableView Width="100%">
            <CommandItemTemplate>
                <asp:Button ID="DownloadPDF" runat="server" CommandName="ExportToPdf" CssClass="pdfButton" />
            </CommandItemTemplate>
            <Columns>
                <telerik:GridBoundColumn FilterControlWidth="105px" DataField="unikID" HeaderText="unikID"
                    Visible="false" SortExpression="unikID" UniqueName="unikID" CurrentFilterFunction="Contains"
                    ShowFilterIcon="false" AutoPostBackOnFilter="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn FilterControlWidth="105px" DataField="Navn" HeaderText="Navn"
                    Visible="true" SortExpression="Navn" UniqueName="Navn" CurrentFilterFunction="Contains"
                    ShowFilterIcon="false" AutoPostBackOnFilter="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn FilterControlWidth="105px" DataField="produktNavn" HeaderText="Produktnavn"
                    Visible="true" SortExpression="produktNavn" UniqueName="produktNavn" CurrentFilterFunction="Contains"
                    ShowFilterIcon="false" AutoPostBackOnFilter="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn FilterControlWidth="105px" DataField="prodkatNavn" HeaderText="Kategori"
                    Visible="true" SortExpression="prodkatNavn" UniqueName="prodkatNavn" AutoPostBackOnFilter="true"
                    CurrentFilterFunction="Contains" ShowFilterIcon="false">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn FilterControlWidth="105px" DataField="placprodNavn" HeaderText="Produktplacering"
                    Visible="true" SortExpression="placprodNavn" UniqueName="placprodNavn" CurrentFilterFunction="Contains"
                    ShowFilterIcon="false" AutoPostBackOnFilter="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn FilterControlWidth="105px" DataField="rekvNr" HeaderText="Rekv. Nr."
                    Visible="true" SortExpression="unikID" UniqueName="rekvNr" CurrentFilterFunction="Contains"
                    ShowFilterIcon="false" AutoPostBackOnFilter="true">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
        <PagerStyle Mode="Slider" />
    </telerik:RadGrid>

Code behind:
public partial class SimpleViewExport : System.Web.UI.UserControl
{
    private string _con, _query;
 
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.FilterCommandName)
        {
            BindGrid();
        }
        if (e.CommandName == RadGrid.ExportToPdfCommandName)
        {
            BindGrid();
        }
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {
        Sitecore.Data.Fields.ReferenceField conlink = Sitecore.Context.Item.Fields["Connectionstring"];
        var constringfieldprefix = ConfigurationManager.AppSettings["ConnectionStringFieldPrefix"];
        _con = conlink != null ? conlink.TargetItem[constringfieldprefix + "connectionstring"] : null;
        _query = Sitecore.Context.Item["Select statement"];
 
        if (string.IsNullOrEmpty(_con) || string.IsNullOrEmpty(_query))
        {
            this.Visible = false;
            return;
        }
 
        if (Sitecore.Context.Item["Title"] != null)
            RadGrid1.ExportSettings.Pdf.PageTitle = Sitecore.Context.Item["Title"];
 
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
 
    public DataTable GetDataTable(string query)
    {
        SqlConnection conn = new SqlConnection(_con);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);
 
        DataTable myDataTable = new DataTable();
 
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
 
        return myDataTable;
    }
 
    protected void BindGrid()
    {
        RadGrid1.DataSource = GetDataTable(_query);
        RadGrid1.DataBind();
    }
}

0
Jakob
Top achievements
Rank 1
answered on 21 Sep 2011, 10:24 AM
Hi again

Tried your code once again, but only this time, I disabled viewstate on my radgrid, and now it works perfectly.

Thank you so much for your help :-)
Tags
Grid
Asked by
Jakob
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Jakob
Top achievements
Rank 1
Share this question
or