Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 2nd, 2007, 02:19 PM
Friend of Wrox
Join Date: Apr 2005
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to print from a dynamic gridview at runtime.

Certain website applications contain asp:gridview controls. Paging and sorting are ‘built in’ but printing is not. It would be nice if there was an easy way to dynamically print a hardcopy of the content of a gridview control as it shown on a users screen. The problem is that sorting and highlighting the gridview lines takes place at the server, while printing takes place on the client machine. It turns out there is a way, but it involves a bit of code -- an example of which is shown here.

In the application for which the code below was written, a user is allowed to click on any column heading in a populated gridview and the gridview would be sorted accordingly. This is standard behavior. Initially there is only room on the webpage to show 10 lines in the gridview. Once the user starts to sort the gridview, the gridview reverts to a very long gridview allowing the user to scroll vertically wherever he likes, to located sorted lines.

In addition to the standard behavior, this particular application wanted the entire column to be italicized and made bold, so that users could easily identify and see how the gridview printout was sorted. Further, there was another button (asp:button) on the page that a user could click to print the gridview showing the current sort with the italicized, bold column.

Implementation technique:

1. Assuming the gridview has already been filled with data, a user clicks the column to be sorted and the GridView1_Sorting event handler executes. Paging and PageSize are reset so that further events on the gridview will affect all the data in the gridview, not just the few lines shown on a single page. Note that the sort column (header) is saved in the session object.

2. The PreRender event handler determines if the session object has been set (to remember which column was sorted), due to a user gridview column header click. If it has, then it calls a sub to make the entire column italicized and bold. A gridview has many event handlers. The PreRender handler was the first one found that was able to do a full post (round trip) to the server, retaining and showing the italicized and bolded column.

3. Eventually, a user will want to print a hardcopy of the gridview showing whatever sort sequence was selected. There is an asp:button for this. This
application chose to use a one line javascript function to handle the printing as shown below. An asp:button has the capability of both a client side function (onclientclick) and server side (onclick) function. The client side function is an easy way to produce a local (client machine) printout.

4. I wish to publicly thank Imar Spaanjarrs (Imar) and Peter (planoie) and (dparsons,for a related thread), for their help on making this technique possible. I had been struggling for a week on this. You guys are a valuable resource for those of us still climbing the learning ladder.


    Protected Sub GridView1_Sorting(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
    â€˜The gridview is initialized for paging and with property PageSize = ‘10 until this event changes those properties.
        GridView1.AllowPaging = False
        GridView1.PageSize = 20000
        HttpContext.Current.Session("ColumnToItalicize") = e.SortExpression
    End Sub

    Protected Sub GridView1_PreRender(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles GridView1.PreRender
        If HttpContext.Current.Session("ColumnToItalicize") <> Nothing Then
        End If
    End Sub

    Private Sub MakeSortColumnBoldItalicized()
        Dim sColName As String = HttpContext.Current.Session("ColumnToItalicize")
        Dim iColNumber As Integer
        Select Case sColName
            Case Is = "WorkDate"
                iColNumber = 0
            Case Is = "WorkTime"
                iColNumber = 1
            Case Is = "Chapter"
                iColNumber = 2
            Case Is = "VolunteerFirstName"
                iColNumber = 3
            Case Is = "VolunteerLastName"
                iColNumber = 4
            Case Is = "WorkType"
                iColNumber = 5
            Case Is = "SubmitterFirstName"
                iColNumber = 6
            Case Is = "SubmitterLastName"
                iColNumber = 7
            Case Else
                iColNumber = -1 'Error.
        End Select
        Dim iRowCount As Integer = GridView1.Rows.Count - 1
        For i As Integer = 0 To iRowCount
            GridView1.Rows(i).Cells(iColNumber).Font.Italic = true
            GridView1.Rows(i).Cells(iColNumber).Font.Bold = true
    End Sub

<asp:Button ID="btnPrint" runat="server" Text="Print" Width="120px" OnClientClick="printsortedgridview()"/>

function printsortedgridview()

Similar Threads
Thread Thread Starter Forum Replies Last Post
binding GridView at runtime! veda Visual Studio 2005 3 August 4th, 2008 06:44 AM
Dynamic textbox generation into gridview dharmeshtandel ASP.NET 2.0 Basics 3 December 24th, 2007 02:14 AM
Changing row position of gridview at runtime rameshsamiappan ASP.NET 2.0 Basics 0 September 25th, 2006 10:02 AM
Dynamic data in Gridview lukemack ASP.NET 2.0 Basics 8 May 26th, 2006 03:37 PM
Dynamic addtion of DropDownLists at Runtime DolphinBay ASP.NET 1.0 and 1.1 Professional 13 October 19th, 2005 11:11 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.