Results 1 to 10 of 10

Thread: [RESOLVED] setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Resolved [RESOLVED] setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

    Hi all

    I have a program which displays Microsoft Access Data using a DataGridView. This data can then be printed out using the DataGridPrinter Class which I got from here

    http://www.vbforums.com/showthread.php?t=356115

    The problem is that it only ever displays and prints out the first page!. I have been told that this is because the e.HasMorePages value is being set incorrectly

    The only reference I can find to e.HasMorePages in the DataGridPrinter class is in this statement which lies within a procedure called GridPrintDocument_PrintPage


    Code:
      '\\ If there are more lines to print, set the HasMorePages property to true
            If _CurrentPrintGridLine < GridRowCount() Then
                e.HasMorePages = True
            End If
    Is there any way to make it print out more than one page? 4 pages would be perfect. Can I set up a for loop or something to do that?

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter cla

    I haven't looked at the code for the class you mention, but looking at the snippet you posted, the call to the function GridRowCount() presumably returns the number of rows in your grid.

    This is being compared with the _CurrentPrintGridLine value to decide if there are any more pages to print and sets the .HasMorePages property accordingly.

    If this ends up as 'True', then the PrintPage event will fire again to carry on printing, until .HasMorePages is 'False'.

    Place a couple of Debug.Writelines in there, or set a breakpoint to see what values you are getting.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

    Andy, I tried putting some Debug.Write lines in the DataGridPrinter.vb file but nothing would show up on the output window. I dont know how to do break points

    If it's any help, this is the code in the Print button on the form which displys the contents of the database

    Code:
            If GridPrinter Is Nothing Then
                GridPrinter = New DataGridPrinter(Me.DataGridView1)
            End If
    
            'setting properties of dgview (after adding gridprinter )
            With GridPrinter
                .HeaderText = CStr("Report Dated - " & Today)
                .HeaderHeightPercent = CInt(5)
                .FooterHeightPercent = CInt(0)
                .InterSectionSpacingPercent = CInt(3)
                .PagesAcross = CInt(2)
            End With
    
            'printpreview prinout
            With Me.PrintPreviewDialog1
                .Document = GridPrinter.PrintDocument
                If .ShowDialog = DialogResult.OK Then
                    GridPrinter.Print()
                End If
            End With

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter cla

    Put them just before the check that decides if .HasMorePages should be true or not.

    Try this:
    vb Code:
    1. Debug.Writeline("_CurrPrintGridLine value : ", _CurrentPrintGridLine)
    2. Debug.Writeline("GridRowCount value : ", GridRowCount())
    3. If _CurrentPrintGridLine < GridRowCount() Then
    4.     e.HasMorePages = True
    5. End If

    Note that the 'Debug' lines will only execute if you build and run your app in Debug mode.

    You could put Console.Writeline... instead, but then they will always execute, even if you compile the app in Release mode.

    I would definitely recommend learning how to use a breakpoint. Very useful in debugging your app. It does what it says - 'breaks' execution allowing you to run your app step-by-step, inspect variable values, etc.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

    I tried adding the code you said but I got the following 2 errors befoe I could even build it.

    Code:
    Error	1	Overload resolution failed because no accessible 'WriteLine' can be called without a narrowing conversion:
        'Public Shared Sub WriteLine(value As Object, category As String)': Argument matching parameter 'category' narrows from 'Integer' to 'String'.
        'Public Shared Sub WriteLine(message As String, category As String)': Argument matching parameter 'category' narrows from 'Integer' to 'String'.	F:\Documents and Settings\Tommy\My Documents\Visual Studio 2005\Projects\FinalYearProject\FinalYearProject\DataGridPrinter.vb	475	9	FinalYearProject
    Code:
    Error	2	Overload resolution failed because no accessible 'WriteLine' can be called without a narrowing conversion:
        'Public Shared Sub WriteLine(value As Object, category As String)': Argument matching parameter 'category' narrows from 'Integer' to 'String'.
        'Public Shared Sub WriteLine(message As String, category As String)': Argument matching parameter 'category' narrows from 'Integer' to 'String'.	F:\Documents and Settings\Tommy\My Documents\Visual Studio 2005\Projects\FinalYearProject\FinalYearProject\DataGridPrinter.vb	476	9	FinalYearProject
    you also said I have to run it in debug mode, how would I do that?

  6. #6
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter cla

    Apologies, change the lines to this:
    Code:
    Debug.Writeline("_CurrPrintGridLine value : " & _CurrentPrintGridLine.ToString)
    Debug.Writeline("GridRowCount value : " & GridRowCount.ToString)

    You probably already are running in Debug mode.

    Go to the Build menu, select Configuration manager and check that the Active Solution Configuration is set to Debug.

    Or you can use the build configuration drop-down box on the toolstrip, depending on how your version of VB is set up.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

    Worked like a charm! Output from debug window was

    Code:
    _CurrPrintGridLine value : 1
    GridRowCount value : 1
    The problem is that if i remove the IF statement and just put e.HasMorePages = True then it's stuck in a perpetual state of adding more pages when I click the print button

    I need a way to set the e.HasMorePages = True for four pages then set it to false

  8. #8
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter cla

    What is this output telling you, and compare it against the 'If' check.
    Code:
    _CurrPrintGridLine value : 1
    GridRowCount value : 1
    Code:
    If _CurrentPrintGridLine < GridRowCount() Then
        e.HasMorePages = True
    End If
    Is the value of _CurrentPrintGridLine less than the value returned from the call to the GridRowCount() function?

    No it is not, they both have a value of 1, therefore .HasMorePages will never be set to true, and only one page is going to be printed.

    If you permanently set .HasMorePages to true, then it will carry on forever, as you have found out because you are constantly telling the app that more pages are to be printed!

    You are going to have to show us what code you have in the GridRowCount function. I would guess there might be an error in there because it is returning a value of 1, and I am assuming your grid has more than one row in it.

    Or, failing that, you will have to set up a counter variable that increments each time a page has been printed, and stop when it reaches a count of 4, but let's see what you have in the function first.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    61

    Re: setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

    Andy I seem to have gotten it working somehow. I set the initial value of GridRowCount to 0 and it seems to be fine now. Thanks for all your help!
    Last edited by micki_free; Jan 4th, 2009 at 05:35 PM.

  10. #10
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [RESOLVED] setting e.HasMorePages when Printing out a DataGridView using DataGridPrinter class

    The function GridRowCount() should be returning the total number of records in your data table - this is what it uses to decide whether or not it has reached the last record. If it is returning 0 (or 1) and your data table has more than 1 record then something is amiss.

    The code works best if the DataSource of the data grid being printed is a DataTable class - can you try putting your MSAccess data in a Datatable and then using that maybe?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width