?
Solved

Printable Version of a Grid

Posted on 2007-07-25
10
Medium Priority
?
589 Views
Last Modified: 2008-01-09
Hi,
Im using asp.net 2005. I have a grid in my homepage that I want the user to be able to friendly print it.
My homepage has banners and menus that I dont want to have in my printed page, plus the page will be larger than the printed one.
To avoid all that, I need to have the grid show in a new window that is just for printing, and only the grid. How may I do that without having to develop the second page? I need to create it at runtime.
0
Comment
Question by:feesu
  • 4
  • 4
  • 2
10 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 19564430
I use the method found in the paragraph titled "Generating Excel Reports" from http://www.beansoftware.com/ASP.NET-Tutorials/Generating-Excel-Reports.aspx
0
 
LVL 1

Expert Comment

by:ggable313
ID: 19565007
I briefly looked at the code at the link provided and thanks, I bookmarked it to look at later too!  When I did this, however, I didnt want to rerun my query since I wanted the exact same results to be printed.  I didnt want the chance of the grid being different from my print-out.  So, the following code uses the same datagrid that has already been populated.  Make sure that the datagrid is only populated when the page is not a postback.

Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
        If dgReportResults.Items.Count > 0 Then
            ExportToExcel(dgReportResults)
        Else
            lblResults.Visible = True
            lblResults.Text = "There are no records to export, run a new report and then try to export."
        End If
    End Sub

Private Sub ExportToExcel(ByVal grd As DataGrid)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False

        Dim oStringWriter As New System.IO.StringWriter
        Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
        Me.ClearControls(grd)
        grd.RenderControl(oHtmlTextWriter)

        Response.Write(oStringWriter.ToString)
        Response.End()
    End Sub
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 19565032
It isn't the example I wanted you to look at. See the code for the btnToExcelByResponse_Click event. It is outputing a current grid.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:ggable313
ID: 19565144
Ok, in the example, it sets a new datagrid and repopulates that.  I am sure that you can just modify that example to use the same datagrid.  Or, you can try it with just using the datasource of the current grid as the datasource for the new datagrid.  I am not sure if I remember if that works totally, it doesnt present an error in VS2005:
     'grd is the current datagrid
     Dim dg As New DataGrid
     dg.DataSource = grd.DataSource
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 19565700
replace dgGrid with your grid!

    Protected Sub btnToExcelByResponse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnToExcelByResponse.Click
        '   Variables declaration
        'Dim dsExport As New DataSet()
        Dim tw As New System.IO.StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        'Dim dgGrid As New DataGrid()
 
        'dgGrid.DataSource = getData()
 
        '   Report Header
        hw.WriteLine("<b><u><font size='5'> Student Marking Report </font></u></b>")
 
        '   Get the HTML for the control.
        YourGrid.HeaderStyle.Font.Bold = True
        YourGrid.DataBind()
        YourGrid.RenderControl(hw)
 
        '   Write the HTML back to the browser.
        Response.ContentType = "application/vnd.ms-excel"
        Me.EnableViewState = False
        Response.Write(tw.ToString())
        Response.End()
    End Sub
0
 
LVL 1

Expert Comment

by:ggable313
ID: 19565873
But with that, you are binding data to the grid again without specifying the datasource again.  Wont that mess up the data that is already there?  Can you DataBind without specifying a DataSource?

Either way, both examples are very similar.  I modified yours though, to just use the current datagrid's datasource as the datasource for the new datagrid.  Everything else can be the same.

    Protected Sub btnToExcelByResponse_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnToExcelByResponse.Click
        '   Variables declaration
        Dim dsExport As New DataSet()
        Dim tw As New System.IO.StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        Dim dgGrid As New DataGrid()
 
        'Just change it so that it doesnt run the query again.
        dgGrid.DataSource = YourGrid.DataSource
 
        '   Report Header
        hw.WriteLine("<b><u><font size='5'> Student Marking Report </font></u></b>")
 
        '   Get the HTML for the control.
        dgGrid.HeaderStyle.Font.Bold = True
        dgGrid.DataBind()
        dgGrid.RenderControl(hw)
 
        '   Write the HTML back to the browser.
        Response.ContentType = "application/vnd.ms-excel"
        Me.EnableViewState = False
        Response.Write(tw.ToString())
        Response.End()
    End Sub
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 19567918
>>But with that, you are binding data to the grid again

if you read correctly, the line that is setting the datasource has been commented out (and the declaration also).
0
 

Author Comment

by:feesu
ID: 19568689
emoreau,
I tried the code you pasted and got the below exception:


System.Web.HttpException was unhandled by user code
  ErrorCode=-2147467259
  Message="Control 'ctl02_GRD_header_ctl01' of type 'o' must be placed inside a form tag with runat=server."
  Source="System.Web"
  StackTrace:
       at System.Web.UI.Page.VerifyRenderingInServerForm(Control control)
       at System.Web.UI.WebControls.LinkButton.AddAttributesToRender(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
       at C1.Web.C1WebGrid.o.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
       at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
       at C1.Web.C1WebGrid.m.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
       at C1.Web.C1WebGrid.C1GridItem.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at C1.Web.C1WebGrid.k.a(HtmlTextWriter A_0)
       at C1.Web.C1WebGrid.d.a(HtmlTextWriter A_0)
       at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
       at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
       at System.Web.UI.Control.Render(HtmlTextWriter writer)
       at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
       at C1.Web.C1WebGrid.C1WebGrid.Render(HtmlTextWriter writer, Boolean renderDiv)
       at C1.Web.C1WebGrid.C1WebGrid.Render(HtmlTextWriter writer)
       at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
       at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
       at Broker_MarketQuotes.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\AlSeefBroker\DesktopModules\Broker\Broker_MarketQuotes.ascx.vb:line 275
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
0
 

Author Comment

by:feesu
ID: 19568742
guys,
I don't want to export to Excel!! I have the code that exports to excel and it works fine.

I want to export the grid into a new html page that has only the grid. Pure html and not excel or anything that needs installation!
0
 
LVL 1

Accepted Solution

by:
ggable313 earned 1500 total points
ID: 19569046
1) IF your dataset isnt big, you could put it in a session variable as a datatable, etc.  Then both datagrids would be populated with that.  Although, performance will be affected with a good bit of data.

2) Otherwise, How do you store/pass criteria values?  Do you use query string parameters to pass criteria values, have them in session variables, etc?  You could put the code to get your dataset in a class that is separate from either of the pages (original page and printer friendly page), then just call that code with the same criteria or parameters.  In this case you would have 2 pages, each with a datagrid that is using the same code to populate them.

I guess it depends on the size of the data.  If it is only a little bit, putting it in a session variable ensures you have the same exact data populating both datagrids.  If performance is an issue, then I think you will have to repopulate the second datagrid - just use the same code and filter criteria and you will get the same results.  I dont know of a way of passing between pages easily like what you are wanting another way.


A question for you emoreau:  I did notice that the line of code that specifies the new datagrid is commented out, but the Databind statement isnt:
        '   Get the HTML for the control.
        YourGrid.HeaderStyle.Font.Bold = True
        YourGrid.DataBind()
        YourGrid.RenderControl(hw)

I didnt see where YourGrid was specifying a datasource again, and that is what is confusing me.  I was curious if this DataBind would mess up the data that is there already, or if it just binds the header row to what is there, etc.  I was under the impression you needed to specify the datasource for databind to work - is that not the case in all situations?    Thanks!


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question