473,545 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_DataBin ding) in Page1.aspx
to set DataSource

toPrint.EnableV iewState = false;
toPrint.AutoGen erateColumns = false;

toPrint.CssClas s = origin.CssClass ;
toPrint.HeaderS tyle.CssClass = origin.HeaderSt yle.CssClass;
toPrint.Alterna tingItemStyle.C ssClass = origin.Alternat ingItemStyle.Cs sClass;

foreach (DataGridColumn dgc in origin.Columns)
{
toPrint.Columns .Add(dgc); // ¡Also works with Templated Columns!
}
toPrint.DataSou rce = origin.DataSour ce; // Set the same DataSource.
toPrint.DataBin d();

pg.EnableViewSt ate = false;
pg.Controls.Add (frm);
frm.Attributes. Add ("runat", "server");
frm.Controls.Ad d (toPrint);
pg.DesignerInit ialize();
pg.RenderContro l(htmlWrite);

Page.RegisterSt artupScript("iw c_toprint", "<div id='divPrint'
style='VISIBILI TY: hidden; OVERFLOW: hidden; HEIGHT: 1px'>"
+ stringWrite.ToS tring() + "</div><script
language=javasc ript>CallPrint( 'divPrint');</script>");

strHTML.WriteLi ne("<script language=javasc ript>");
strHTML.WriteLi ne("function CallPrint(strid )");
strHTML.WriteLi ne("{");
strHTML.WriteLi ne("var prtContent = document.getEle mentById(strid) ;");
strHTML.WriteLi ne("var WinPrint = window.open('pr int.aspx','','' );");
strHTML.WriteLi ne("WinPrint.fo cus();");
strHTML.WriteLi ne("}");
strHTML.WriteLi ne("</script>");

Page.RegisterCl ientScriptBlock ("iwc_printfun" , strHTML.ToStrin g());
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="javas cript">
function copy()
{
var prtContent = opener.document .getElementById ('divPrint');
var dest = document.getEle mentById('res')

dest.innerHTML = prtContent.inne rHTML;
// 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.P age.LoadPageSta teFromPersisten ceMedium()
System.Web.UI.P age.LoadPageVie wState()
System.Web.UI.P age.ProcessRequ estMain()

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 1461

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

Similar topics

2
1439
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
1495
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 want to print all the pages in that datagrid, as I have used paging in the datagrid (there is an underlying datatable which holds the data).
5
2015
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 one row, all of them were updated so i immediatelly figured out that i have to include the id of every entry in the update statement. This is where...
2
1394
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 attempt to FieldCount because datareader is already closed (sorry, this error message is translated from german) when trying to do the databinding.
4
2117
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 Delete button on the end of each row. I am unable to gain access to the event when the button is clicked. I don't fully understand how the click...
0
955
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 perfect...except when i print the page. The problem is when i print the page, the gridlines of the datagrid are well printed only in the first page...
3
1887
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 grid. The rest of the pages do not contain any lines.
1
3500
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 also if I click on a data bound checkbox cell. But it fails with null reference exception if I try to read the value on an unbound cell.
2
1513
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 between all of the grids is the query and the widows form it is on. I basicly just copied the form changing the SQL for the datasource. any way so here...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.