473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problem trying to print a DataGrid

Hello,

I founded some examples of WebControl printing in internet and tryed to have
them working for a DataGrid, something not very complicated.

I have a page, Page1.aspx with a DataGrid (paginated) that i want to get
alone in page Print.aspx not paginated. Both pages will have the same CSS
file so they will look identical except of the paginating.
There is a Button (Print) in Page1.aspx with this code:

StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Page pg = new Page();
HtmlForm frm = new HtmlForm();
StringWriter strHTML = new StringWriter();
DataGrid toPrint = new DataGrid();

origin.DataBind(); //Fires an event (origin_DataBinding) in Page1.aspx
to set DataSource

toPrint.EnableViewState = false;
toPrint.AutoGenerateColumns = false;

toPrint.CssClass = origin.CssClass;
toPrint.HeaderStyle.CssClass = origin.HeaderStyle.CssClass;
toPrint.AlternatingItemStyle.CssClass = origin.AlternatingItemStyle.CssClass;

foreach (DataGridColumn dgc in origin.Columns)
{
toPrint.Columns.Add(dgc); // ¡Also works with Templated Columns!
}
toPrint.DataSource = origin.DataSource; // Set the same DataSource.
toPrint.DataBind();

pg.EnableViewState = false;
pg.Controls.Add (frm);
frm.Attributes.Add ("runat", "server");
frm.Controls.Add (toPrint);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);

Page.RegisterStartupScript("iwc_toprint", "<div id='divPrint'
style='VISIBILITY: hidden; OVERFLOW: hidden; HEIGHT: 1px'>"
+ stringWrite.ToString() + "</div><script
language=javascript>CallPrint('divPrint');</script>");

strHTML.WriteLine("<script language=javascript>");
strHTML.WriteLine("function CallPrint(strid)");
strHTML.WriteLine("{");
strHTML.WriteLine("var prtContent = document.getElementById(strid);");
strHTML.WriteLine("var WinPrint = window.open('print.aspx','','');");
strHTML.WriteLine("WinPrint.focus();");
strHTML.WriteLine("}");
strHTML.WriteLine("</script>");

Page.RegisterClientScriptBlock("iwc_printfun", strHTML.ToString());
All of this create a hidden DIV called divPrint with the HTML for the
DataGrid toPrint, then open a new window to print.aspx with the following
code.

<HEAD>
<script language="javascript">
function copy()
{
var prtContent = opener.document.getElementById('divPrint');
var dest = document.getElementById('res')

dest.innerHTML = prtContent.innerHTML;
// opener.history.back(1);

print();
}
</script>
</HEAD>
<body onload="copiar();">
<br>
<div id='res'>
</div>
</body>
All works but i receive an error in Page1.aspx when i try to continue
working with it (print contents close print.aspx and do something else to
send a POST).
The error message is:

[HttpException (0x80004005): The View State is invalid for this page and
might be corrupted.]
System.Web.UI.Page.LoadPageStateFromPersistenceMed ium()
System.Web.UI.Page.LoadPageViewState()
System.Web.UI.Page.ProcessRequestMain()

A workaround (i dont like) is to uncomment this line in print.aspx.
// opener.history.back(1);

But i'd like to find a better solution.
I think that, for example, doing this:
foreach (DataGridColumn dgc in origin.Columns)
{
toPrint.Columns.Add(dgc); // ¡Also works with Templated Columns!
}
is not very safe, hehe...

Thanks a lot for your help.

Best regards,

Paco Ferre

Nov 19 '05 #1
0 1440

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MJay | last post by:
can someone provide me some codes that can print out a datagrid after i have loaded the datagrid with records in a DataTable?
1
by: Paresh | last post by:
Hi I need to Print asp:datagrid with formatting, which will avoid my writing of crystal reports and can directly print with click of a button. I do not just want to print the current page, I...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
2
by: wolfgang wagner | last post by:
hi all! im trying to edit data in a datagrid (like described here: http://aspnet.4guysfromrolla.com/articles/071002-1.aspx) but if i click the edit button i get the following error invalid...
4
by: Jeff User | last post by:
Hi I tryed to solve this problem over in the framework.asp group, but still am having trouble. Hope someone here can help. using .net 1.1, VS 2003 and C# I have an asp.DataGrid control with a...
0
by: Vitor Mata | last post by:
Please help me on this one... I have one page with one datalist ...and on the item template i have one datagrid ... by other words one datalist populating one datagrid... everything is working...
3
by: Jay | last post by:
Hi, I have this problem with DataGrid printing. I have a page which contains only DataGrid. When i try to print the page, only 1st page contains the verticle and horizontal lines of the...
1
by: Martin | last post by:
Hi, I'm having a problem with a datagridview control when trying to read the value of an unbound checkbox cell. The code below works fine if I click on any text cell, bound or unbound, and...
2
daniel aristidou
by: daniel aristidou | last post by:
Hi i wrote code to print records off a datagrid.the code works on all but one of my data grids. The problem is that loop continues without stopping, Causing the program to crash. The only diff...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.