2 Replies - 1506 Views - Last Post: 05 March 2012 - 11:30 AM Rate Topic: -----

#1 Ozzy18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-October 11

VB.Net printing problem HELP HELP

Posted 03 March 2012 - 05:41 AM

Hello , I need some help with my printing from my data grid.
Basically I can get all the data within the grid present in the "print preview" however they are not within the right columns , and all the data overlaps , making the printout look very messy ! :(

I need to be able to get all the data into their specific columns , e.g all the names under the name column .

This is my code so dar =] ( its rather long , so please bare with me !)

PrintDocument1.DefaultPageSettings.Landscape = True

        Dim Myfont As New Font("Courier New", 10, FontStyle.Regular)
        Dim MyFontSmall As New Font("Courier New", 8, FontStyle.Regular)
        Dim MyFormat As String = "{0,-6}{1,-12}{2,-12}{3,-8}{4,-23}{5,-12}{6,-20}{7,-20}{8,-6}"
        Dim MyFormat2 As String = "{0,-20}{1,-20}{2,-20}{3,-20}{4,-12}{5,-12}{6,-12}{7,-12}{8,-6}"
        Dim LineToprint As String
        Dim X, Y, FontHeight As Integer
        X = 10
        Y = 50
        FontHeight = Myfont.GetHeight(e.Graphics)

        e.Graphics.DrawString("List Of all customers", Myfont, Brushes.Black, X, Y)
        Y = Y + FontHeight
        e.Graphics.DrawString("", Myfont, Brushes.Black, X, Y)
        Y = Y + FontHeight
        LineToprint = String.Format(MyFormat, "Name", "Last Name", "Address", "Email", "House phone Number", "Mobile", "Size Of pool", "Size Of sattelite", "CustomerID")
        e.Graphics.DrawString(LineToprint, Myfont, Brushes.Black, X, Y)
        Y = Y + FontHeight
        e.Graphics.DrawString("", Myfont, Brushes.Black, X, Y)

        Dim DtCustomers As New DataTable()
        Dim CustomerColumnIndex, CustomerRowIndex, index As Short
        DtCustomers = CustomersDs.Tables(0)

        For CustomerColumnIndex = 0 To DtCustomers.Rows.Count - 1
            Y = Y + FontHeight
            X = 10

            For CustomerRowIndex = 0 To DtCustomers.Columns.Count - 1

                LineToprint = DtCustomers.Rows(CustomerColumnIndex)(CustomerRowIndex).ToString

                e.Graphics.DrawString(LineToprint, MyFontSmall, Brushes.Black, X, Y)

                X = X + 100

            Next CustomerRowIndex

        Next CustomerColumnIndex

    End Sub

This post has been edited by AdamSpeight2008: 03 March 2012 - 05:46 AM
Reason for edit:: Please enclose your code inside the CODETAGS

Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net printing problem HELP HELP

#2 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: VB.Net printing problem HELP HELP

Posted 03 March 2012 - 01:55 PM

can you show your print preview print screen
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: VB.Net printing problem HELP HELP

Posted 05 March 2012 - 11:30 AM

Your columns will have to probably be specified using an X coordinate value. You can create a string with spaces and then match up your values by aligning them at the X coordinate of the start of each column name but I would suggest breaking down the columns and adding them with the x,y cooridnates. Here's why.

The next problem you have is that you have more data than can fit on a single page. Therefore you will need to keep track of where the bottom of the page is and when that limit is reached execute an e.HasMorePages = True. This is where it kind of gets confusing. The moment you trigger e.HasMorePages the entire PrintDocument is re-ran. Read that again. The entire printdocument is re ran. This means that if you are down 40 rows in your for loop. It will start back at the beginning. SO you need a variable that keeps track of your last iteration of the for...next loop you're using and... instead of for x = 0 to listview.items.count -1 you need to use that stored variable. for x = LastIterationNumber to ListView1.Items.Count -1.

WARNING: Frustration will set in, you will pull out many hairs but I can tell you this much, to save paper keep the print preview and don't print the document until the print preview looks correct ;)

Seriously though, just remember that when you trigger a new page with e.hasmorepages it's like calling the printdocument sub as if you had never even called it before.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1