Printable Version of a Grid

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.
feesuAsked:
Who is Participating?
 
ggable313Connect With a Mentor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ggable313Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ggable313Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
ggable313Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
>>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
 
feesuAuthor Commented:
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
 
feesuAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.