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

Hide image when exporting grid to PDF

12 Answers 139 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vitaly
Top achievements
Rank 1
Iron
Iron
Vitaly asked on 24 Sep 2012, 04:21 PM
Good afternoon,
I have created a image inside a column in code behind file:

protected

 

 

void CfnReportGrid_PreRender(object sender, EventArgs e)

 

{

 

 

RadGrid CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

 

 

 

GridHeaderItem headerItem = (GridHeaderItem)CFNGrid.MasterTableView.GetItems(GridItemType.Header)[0];

 

 

 

Image img = new Image();

 

img.ID =

 

"PRImg";

 

img.ImageUrl =

 

"~/Image/Header_Info.jpg";

 

img.ImageAlign =

 

ImageAlign.Bottom;

 

img.ToolTip =

 

"Employee FileNo";

 

img.Width =

 

new Unit(10);

 

img.Height =

 

new Unit(9);

 

headerItem[

 

"EIS_ID"].Controls.AddAt(1, img);

 


When I export this grid to PDF I want to hide this image.In ItemCreated event I try to do this:

protected

 

 

void CfnReportGrid_ItemCreated(object sender, GridItemEventArgs e)

 

 

 

 

if(IsExport)

 

RadGrid

 

 

CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

 

 

 

GridHeaderItem headerItem = (GridHeaderItem)CFNGrid.MasterTableView.GetItems(GridItemType.Header)[0];

 

 

 

Image img = (Image)headerItem.FindControl("PRImg");

 

img.Visible =

 

false;

But gettin runtime error:
Object reference not set to an instance of an object.

Please help me to resolve this issue.
Thanks so much

 

12 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2012, 05:07 PM
Hello,

Please try with below code snippet.

protected void ExporttoPdf_Click1(object sender, ImageClickEventArgs e)
        {
            RadGrid1.MasterTableView.ExportToPdf();
        }


public bool IsExport = false;
 
       protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
       {
           if (e.CommandName == RadGrid.ExportToPdfCommandName)
           {
               IsExport = true;
           }
       }
 
 
       protected void RadGrid1_PreRender1(object sender, EventArgs e)
       {
           if (IsExport == false)
           {
               GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
               Image img = new Image();
               img.ID = "PRImg";
               img.ImageUrl = "~/tempdata/Myphoto.jpg";
               img.ImageAlign = ImageAlign.Bottom;
               img.ToolTip = "Employee FileNo";
               img.Width = new Unit(10);
               img.Height = new Unit(9);
               headerItem["ID"].Controls.AddAt(1, img);
           }
       }


Thanks,
Jayesh Goyani
0
Vitaly
Top achievements
Rank 1
Iron
Iron
answered on 24 Sep 2012, 05:15 PM
Thanks for your quick responding but "Export to PDF" ImageButton located outside Radgrid .When I click button ItemCommand not firing:
here is my aspx page:

<

 

 

asp:Panel ID="Panel1" runat="server">

 

 

 

<telerik:RadPanelBar ID="CfnReport" ExpandMode="MultipleExpandedItems" runat="server" Width="98%" BorderWidth="1px">

 

 

 

 

<Items>

 

 

 

<telerik:RadPanelItem Expanded="true" Text="CFN REPORT" runat="server" Selected="true" Font-Bold="false" Font-Size="13" Font-Names="Verdana" >

 

 

 

<Items>

 

 

 

 

<telerik:RadPanelItem Value="CfnDataRpt" runat="server">

 

 

 

 

<ItemTemplate>

 

 

 

<asp:ImageButton ID="ExporttoPdf" BackColor="Transparent" runat="server"

 

 

 

ImageUrl="~/Image/Export_To_Pdf4.bmp"

 

 

 

onclick="ExporttoPdf_Click1" ToolTip="Export to Pdf" Height="20px"

 

 

 

BorderWidth="2px" />

 

 

 

 

<telerik:RadGrid ID="CfnReportGrid" runat="server" GridLines="None" Skin="Office2010Blue"

 

 

 

AllowPaging="true" AllowSorting="true"

 

 

 

onexcelmlexportrowcreated="CfnReportGrid_ExcelMLExportRowCreated"

 

 

 

onexcelmlexportstylescreated="CfnReportGrid_ExcelMLExportStylesCreated"

 

 

 

onitemcommand="CfnReportGrid_ItemCommand"

 

 

 

onitemcreated="CfnReportGrid_ItemCreated"

 

 

 

onneeddatasource="CfnReportGrid_NeedDataSource"

 

 

 

onpageindexchanged="CfnReportGrid_PageIndexChanged"

 

 

 

onpagesizechanged="CfnReportGrid_PageSizeChanged"

 

 

 

onpdfexporting="CfnReportGrid_PdfExporting"

 

 

 

onsortcommand="CfnReportGrid_SortCommand"

 

 

 

onprerender="CfnReportGrid_PreRender" >

 

 

 

 

<ExportSettings FileName="CfnUserReport" OpenInNewWindow="true" IgnorePaging="true" ExportOnlyData="true">

 

 

 

<Excel Format="ExcelML" FileExtension="xls" />

 

 

 

<Pdf FontType="Subset" PaperSize="Letter" />

 

 

 

</ExportSettings>

 

 

 

<MasterTableView AutoGenerateColumns="false" DataKeyNames="FNDG_ISSUE">

 

 

 

<CommandItemTemplate>

 

 

 

<asp:Button ID="Btn" runat="server" Text="Button"/>

 

 

 

</CommandItemTemplate>

 

 

 

<RowIndicatorColumn>

 

 

 

<HeaderStyle Width="1px" />

 

 

 

</RowIndicatorColumn>

 

 

 

<ExpandCollapseColumn>

 

 

 

<HeaderStyle Width="1px" />

 

 

 

</ExpandCollapseColumn>

 

 

 

<Columns>

 

 

 

<telerik:GridBoundColumn HeaderText="EIS-Id" DataField="EIS_ID" UniqueName="EIS_ID" ReadOnly="true" SortExpression="EIS_ID" HeaderButtonType="TextButton" >

 

 

 

<HeaderStyle Width="1px" />

 

 

 

<ItemStyle Width="1px" />

 

 

 

</telerik:GridBoundColumn>

 

 

 

<telerik:GridBoundColumn HeaderText="Name" DataField="NAME" UniqueName="NAME" ReadOnly="true" SortExpression="NAME" HeaderButtonType="TextButton">

 

 

 

<HeaderStyle Width="1px" />

 

 

 

<ItemStyle Width="1px" />

 

 

 

</telerik:GridBoundColumn>

 

 

 

 

</Columns>

 

 

 

</MasterTableView>

 

 

 

</telerik:RadGrid>

 

 

 

</ItemTemplate>

 

 

 

</telerik:RadPanelItem>

 

 

 

</Items>

 

 

 

</telerik:RadPanelItem>

 

 

 

</Items>

 

 

 

</telerik:RadPanelBar>

Please help me out.Thanks.

 

0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2012, 05:23 PM
Hello,

I have updated above code snippet.

Thanks,
Jayesh Goyani
0
Vitaly
Top achievements
Rank 1
Iron
Iron
answered on 24 Sep 2012, 05:27 PM
Thanks again but I do have this event already:

bool

 

 

IsExport = false;

 

 

 

protected void ExporttoPdf_Click1(object sender, ImageClickEventArgs e)

 

{

 

 

RadGrid CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

 

IsExport =

 

true;

 

CFNGrid.MasterTableView.BorderStyle =

 

BorderStyle.None;

 

CFNGrid.MasterTableView.GridLines =

 

GridLines.Vertical;

 

CFNGrid.MasterTableView.Columns[0].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[1].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[2].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[3].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[4].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[5].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[6].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[7].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[8].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[9].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[10].HeaderStyle.Width =

 

Unit.Pixel(45);

 

CFNGrid.MasterTableView.Columns[11].Visible =

 

false;

 

CFNGrid.MasterTableView.ExportToPdf();

 

}


When I click this ImageButton Itemcommand not firing.

Thanks so much..
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2012, 05:52 PM
Hello,

Please try with below code snippet.

public bool IsExport = false;
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            IsExport = true;
            RadGrid1.MasterTableView.ExportToPdf();
        }
 
        

 
        protected void RadGrid1_PreRender1(object sender, EventArgs e)
        {
            if (IsExport == false)
            {
                GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
                Image img = new Image();
                img.ID = "PRImg";
                img.ImageUrl = "~/tempdata/Myphoto.jpg";
                img.ImageAlign = ImageAlign.Bottom;
                img.ToolTip = "Employee FileNo";
                img.Width = new Unit(10);
                img.Height = new Unit(9);
                headerItem["ID"].Controls.AddAt(1, img);
            }
        }
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
       {
           dynamic data = new[] {
             new { ID = 1, Name ="aaa",country="c1",state="s1"},
             new { ID = 2, Name = "bbb",country="c2",state="s2"},
             new { ID = 3, Name = "ccc",country="c3",state="s3"},
             new { ID = 4, Name = "ddd",country="c4",state="s4"},
              new { ID = 5, Name ="eee",country="c5",state="s5"},
              new { ID = 6, Name ="aaa",country="c6",state="s6"},
             new { ID = 7, Name = "bbb",country="c7",state="s7"},
             new { ID = 8, Name = "ccc",country="c8",state="s8"},
             new { ID = 9, Name = "ddd",country="c9",state="s9"},
              new { ID = 10, Name ="eee",country="c10",state="s10"}
           };
           RadGrid1.DataSource = data;
       }
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
 
           EnableLinqExpressions="false" onprerender="RadGrid1_PreRender1"
           >
           <ExportSettings OpenInNewWindow="true"></ExportSettings>
           <MasterTableView CommandItemDisplay="Top">
               <CommandItemSettings ShowExportToPdfButton="true" />
               <Columns>
                   <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                   </telerik:GridBoundColumn>
                  
                   
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
       <br />
       <asp:Button ID="Button1" runat="server" Text="Export 2 pdf" OnClick="Button1_Click" />



Thanks,
Jayesh Goyani
0
Vitaly
Top achievements
Rank 1
Iron
Iron
answered on 24 Sep 2012, 05:57 PM
Hi,
I am doing exactly the same thing but I do have ItemCommand event:

protected

 

 

void CfnReportGrid_ItemCommand(object sender, GridCommandEventArgs e)

 

{

}
when I click on "export to pdf" button CfnReportGrid_ItemCommand not firing...

Thanks for your help.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2012, 06:00 PM
Hello,

Please try with above demo in other /separate page.

And let me know its work for you or not.

Because above code is worked perfectly in my project/page.

Thanks,
Jayesh Goyani
0
Vitaly
Top achievements
Rank 1
Iron
Iron
answered on 24 Sep 2012, 06:05 PM
Hi,
I do have NeedDataSource event:

protected

 

 

void CfnReportGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)

 

{

 

 

RadGrid CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

 

CFNGrid.DataSource = Session[

 

"CFNDataReport"];

 

}


I have button to PDF just above RadGrid:

 

 

<telerik:RadPanelBar ID="CfnReport" ExpandMode="MultipleExpandedItems" runat="server" Width="98%" BorderWidth="1px">

 

 

 

 

<Items>

 

 

 

<telerik:RadPanelItem Expanded="true" Text="CFN REPORT" runat="server" Selected="true" Font-Bold="false" Font-Size="13" Font-Names="Verdana" >

 

 

 

<Items>

 

 

 

 

<telerik:RadPanelItem Value="CfnDataRpt" runat="server">

 

 

 

 

<ItemTemplate>

 

 

 

<div id="exportDiv" style="position:relative; left :550px;">

 

 

 

<asp:ImageButton ID="ExporttoPdf" BackColor="Transparent" runat="server"

 

 

 

ImageUrl="~/Image/Export_To_Pdf4.bmp"

 

 

 

onclick="ExporttoPdf_Click1" ToolTip="Export to Pdf" Height="20px"

 

 

 

BorderWidth="2px" />

 

 

 

</div>

 

 

 

 

<telerik:RadGrid ID="CfnReportGrid" runat="server" GridLines="None" Skin="Office2010Blue"

 

 

 

AllowPaging="true" AllowSorting="true"

 

 

 

onexcelmlexportrowcreated="CfnReportGrid_ExcelMLExportRowCreated"

 

 

 

onexcelmlexportstylescreated="CfnReportGrid_ExcelMLExportStylesCreated"

 

 

 

onitemcommand="CfnReportGrid_ItemCommand"

 

 

 

onitemcreated="CfnReportGrid_ItemCreated"

 

 

 

onneeddatasource="CfnReportGrid_NeedDataSource"

 

 

 

onpageindexchanged="CfnReportGrid_PageIndexChanged"

 

 

 

onpagesizechanged="CfnReportGrid_PageSizeChanged"

 

 

 

onpdfexporting="CfnReportGrid_PdfExporting"

 

 

 

onsortcommand="CfnReportGrid_SortCommand"

 

 

 

onprerender="CfnReportGrid_PreRender" >

 

 

 

 

<ExportSettings FileName="CfnUserReport" OpenInNewWindow="true" IgnorePaging="true" ExportOnlyData="true">

 

 

 

<Excel Format="ExcelML" FileExtension="xls" />

 

 

 

<Pdf FontType="Subset" PaperSize="Letter" />

 

 

 

</ExportSettings>

 

 

 

<MasterTableView AutoGenerateColumns="false" DataKeyNames="FNDG_ISSUE">

 


But still ItemCommand not firing.
Thanks
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2012, 06:07 PM
Hello,

There is no need to fire itemcommand event.

Please check my below code link for that.

http://www.telerik.com/community/forums/aspnet-ajax/general-discussions/hide-image-when-exporting-grid-to-pdf.aspx#2303590

To solve your issue : when we click on exporttopdf button at that time we have  to avoid add image code.

for that you can also take viewstate varilable/property.

Thanks,
Jayesh Goyani
0
Vitaly
Top achievements
Rank 1
Iron
Iron
answered on 24 Sep 2012, 06:11 PM
I am sorry as I undestand I do nto need itemcomand event but where I have to make this image visible=false?In what event?
Sorry about that but I am new to WEb programming and got a little confuse.

Thanks again.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Sep 2012, 06:23 PM
Hello,

Also try with below code snippet.

protected void Button1_Click(object sender, EventArgs e)
        {
            Session["IsExport"] = true;
            RadGrid1.MasterTableView.ExportToPdf();
        }
 
        protected void RadGrid1_PreRender1(object sender, EventArgs e)
        {
            bool IsExported = false;
 
            if (Session["IsExport"] != null)
            {
                bool.TryParse(Session["IsExport"].ToString(), out IsExported);
            }
            if (IsExported == false)
            {
                GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
                Image img = new Image();
                img.ID = "PRImg";
                img.ImageUrl = "~/tempdata/Myphoto.jpg";
                img.ImageAlign = ImageAlign.Bottom;
                img.ToolTip = "Employee FileNo";
                img.Width = new Unit(10);
                img.Height = new Unit(9);
                headerItem["ID"].Controls.AddAt(1, img);
            }
            else
            {
                Session["IsExport"] = false;
            }
        }
 
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
              new { ID = 1, Name ="aaa",country="c1",state="s1"},
              new { ID = 2, Name = "bbb",country="c2",state="s2"},
              new { ID = 3, Name = "ccc",country="c3",state="s3"},
              new { ID = 4, Name = "ddd",country="c4",state="s4"},
               new { ID = 5, Name ="eee",country="c5",state="s5"},
               new { ID = 6, Name ="aaa",country="c6",state="s6"},
              new { ID = 7, Name = "bbb",country="c7",state="s7"},
              new { ID = 8, Name = "ccc",country="c8",state="s8"},
              new { ID = 9, Name = "ddd",country="c9",state="s9"},
               new { ID = 10, Name ="eee",country="c10",state="s10"}
            };
            RadGrid1.DataSource = data;
        }


As you have add image in prerender event.
We have to put condition for image visibility in prerender event.

As per my idea its better if we do not add image on at export time. Because it is useless to first add image and then set visible="false".

Thanks,
Jayesh Goyani
0
Vitaly
Top achievements
Rank 1
Iron
Iron
answered on 24 Sep 2012, 07:41 PM
Thanks so much Jayesh,
it is working now.
Tags
General Discussions
Asked by
Vitaly
Top achievements
Rank 1
Iron
Iron
Answers by
Jayesh Goyani
Top achievements
Rank 2
Vitaly
Top achievements
Rank 1
Iron
Iron
Share this question
or