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.
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
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
Protected Sub GridView1_PreRender(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles GridView1.PreRender
If HttpContext.Current.Session("ColumnToItalicize") <> Nothing Then
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
iColNumber = -1 'Error.
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
<asp:Button ID="btnPrint" runat="server" Text="Print" Width="120px" OnClientClick="printsortedgridview()"/>