Results 1 to 21 of 21

Thread: [RESOLVED] DataTable Problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Resolved [RESOLVED] DataTable Problem

    I have created a datatable and now i need to print it,so i did the following:
    Code:
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            
            Dim xPos As Single = 20
            Dim yPos As Single = 20
            Dim i As Integer = 0
    
            For Each rows As DataRow In dt.Rows
                e.Graphics.DrawString(dt.Rows(i).Item(0).ToString, _detailFont, Brushes.Black, xPos, yPos)
                xPos += e.Graphics.MeasureString(dt.Rows(i).Item(0).ToString, _detailFont).Width
                e.Graphics.DrawString(dt.Rows(i).Item(1).ToString, _detailFont, Brushes.Black, xPos, yPos)
                xPos += e.Graphics.MeasureString(dt.Rows(i).Item(1).ToString, _detailFont).Width
                e.Graphics.DrawString(dt.Rows(i).Item(2).ToString, _detailFont, Brushes.Black, xPos, yPos)
                xPos += e.Graphics.MeasureString(dt.Rows(i).Item(2).ToString, _detailFont).Width
                e.Graphics.DrawString(dt.Rows(i).Item(3).ToString, _detailFont, Brushes.Black, xPos, yPos)
                xPos = 20
                yPos += 20
                i += 1
            Next
    
        End Sub
    The code works fine.But in the above code as you can see that i am printing the each rows content of the datatable one by one and after printing all the contents of a single row i am looping the code again for printing each and every row present in the datatable.
    So i want to know that inspite of printing each and every data present in the row of the datatable,is it possible to print the entire datarow at a time and then loop through all the rows present in the datatable?
    Hope you can understand my problem.Please help
    Thank you

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    or some other alternating ideas to print the entire datatable will also be very helpful........

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataTable Problem

    one way or another you'll have to loop through your dt fields. doesn't seem to be any easier way.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    If you were asked to do the same then may i know how you would have done it?
    Though my code works but i am very interested to know how do the experts would have done the same thing.......

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataTable Problem

    i'm not sure how your formatting works out with your method. i'd measure each row of each field + format the columns appropriately based on the greatest column width

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    Quote Originally Posted by .paul. View Post
    i'm not sure how your formatting works out with your method. i'd measure each row of each field + format the columns appropriately based on the greatest column width
    the formatting part is an issue for me also as i cant understand how to do this,even i cant add the datatable header(i.e,the column names of the datatable) and i need some help from you paul.

    i did this to set the font:
    Code:
     Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            'set the data pointers (if any) to the start position
            'initialise any page number variables you might have
    
            _titleFont = New Font(FontFamily.GenericSerif, 16, FontStyle.Bold, GraphicsUnit.Point)
    
            _detailFont = New Font(FontFamily.GenericSerif, 9, FontStyle.Bold, GraphicsUnit.Point)
    
        End Sub
    but i need your help in measure each row of each field + format the columns appropriately based on the greatest column width.....

    how would you have done this?

    Please help

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    Quote Originally Posted by .paul. View Post
    i'm not sure how your formatting works out with your method. i'd measure each row of each field + format the columns appropriately based on the greatest column width
    as you said,i just changed my code to this:
    Code:
       Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim xPos As Single = 120
    
            Dim yPos As Single = 120
    
            Dim i As Integer = 0
    
            Dim y As Integer = 0
    
            Dim myArray() As Integer = {0, 0, 0, 0}
    
            For i = 0 To dt.Rows.Count - 1
    
                For y = 0 To dt.Columns.Count - 1
    
                    If myArray(y) < e.Graphics.MeasureString(dt.Rows(i).Item(y).ToString, _detailFont).Width + 150 Then
    
                        myArray(y) = e.Graphics.MeasureString(dt.Rows(i).Item(y).ToString, _detailFont).Width + 150
    
                    End If
    
                Next
    
            Next
    
            For i = 0 To dt.Rows.Count - 1
    
                For y = 0 To dt.Columns.Count - 1
    
                    e.Graphics.DrawString(dt.Rows(i).Item(y).ToString, _detailFont, Brushes.Black, xPos, yPos)
    
                    xPos += myArray(y)
    
                Next
    
                xPos = 120
    
                yPos += 50
    
            Next
    
        End Sub
    Is it ok?

    By this code i am adding the datas in the page before printing. Now i want add the datatable header(i.e,the column names of the datatable) on the page,how to go for it?

    Should i use something like this to add each and every columns on the page?
    Code:
    e.Graphics.DrawString("Name", _titleFont, Brushes.Black, xPos, yPos)
    e.Graphics.DrawString("Age", _titleFont, Brushes.Black, xPos, yPos)
    e.Graphics.DrawString("Roll", _titleFont, Brushes.Black, xPos, yPos)
    e.Graphics.DrawString("Sex", _titleFont, Brushes.Black, xPos, yPos)
    Please suggest

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataTable Problem

    you can loop through the datacolumns + print the column name. you should obviously print the column headers before the rest of the datatable:

    vb Code:
    1. For Each c As DataColumn In dt.Columns
    2.     MsgBox(c.ColumnName)
    3. Next

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    hi paul,can you suggest what kind of formatting should i do in the printing page?

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataTable Problem

    try this:

    vb Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.  
    3.     Dim yPos As Single = 120
    4.  
    5.     Dim columnWidths(dt.Columns.Count - 1) As Integer
    6.  
    7.     For x As Integer = 0 To dt.Columns.Count - 1
    8.         columnWidths(x) = Integer.MinValue
    9.     Next
    10.  
    11.  
    12.  
    13.     For x As Integer = 0 To dt.Columns.Count - 1
    14.  
    15.         For y As Integer = 0 To dt.Rows.Count - 1
    16.  
    17.             If columnWidths(x) < e.Graphics.MeasureString(dt.Rows(y).Item(x).ToString, _detailFont).Width + 150 Then
    18.  
    19.                 columnWidths(x) = e.Graphics.MeasureString(dt.Rows(y).Item(x).ToString, _detailFont).Width + 150
    20.  
    21.             End If
    22.  
    23.         Next
    24.  
    25.     Next
    26.  
    27.     For y As Integer = 0 To dt.Rows.Count - 1
    28.  
    29.         For x As Integer = 0 To dt.Columns.Count - 1
    30.  
    31.             If x > 0 Then
    32.  
    33.                 e.Graphics.DrawString(dt.Rows(y).Item(x).ToString, _detailFont, Brushes.Black, columnWidths(x - 1), yPos)
    34.  
    35.             Else
    36.  
    37.                 e.Graphics.DrawString(dt.Rows(y).Item(x).ToString, _detailFont, Brushes.Black, 120, yPos)
    38.  
    39.             End If
    40.  
    41.         Next
    42.  
    43.         yPos += 50
    44.  
    45.     Next
    46.  
    47. End Sub

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    Quote Originally Posted by .paul. View Post
    you can loop through the datacolumns + print the column name. you should obviously print the column headers before the rest of the datatable:

    vb Code:
    1. For Each c As DataColumn In dt.Columns
    2.     MsgBox(c.ColumnName)
    3. Next
    i tried to implement this by doing this code:
    Code:
     Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
          
            Dim xPos As Single = 120
    
            Dim yPos As Single = 120
    
            Dim i As Integer = 0
    
            Dim y As Integer = 0
            Dim myArray() As Integer = {0, 0, 0, 0}
    
            For i = 0 To dt.Rows.Count - 1
    
                For y = 0 To dt.Columns.Count - 1
    
                    If myArray(y) < e.Graphics.MeasureString(dt.Rows(i).Item(y).ToString, _detailFont).Width + 150 Then
    
                        myArray(y) = e.Graphics.MeasureString(dt.Rows(i).Item(y).ToString, _detailFont).Width + 150
    
                    End If
    
                Next
    
            Next
    
            For Each c As DataColumn In dt.Columns
    
                e.Graphics.DrawString(c.ColumnName, _titleFont, Brushes.Black, xPos, yPos)
    
                xPos += myArray(y)
    
            Next
    
            xPos = 120
    
            yPos += 50
    
            For i = 0 To dt.Rows.Count - 1
    
                For y = 0 To dt.Columns.Count - 1
    
                    e.Graphics.DrawString(dt.Rows(i).Item(y).ToString, _detailFont, Brushes.Black, xPos, yPos)
    
                    xPos += myArray(y)
    
                Next
    
                xPos = 120
    
                yPos += 50
    
            Next
    
        End Sub
    i am getting this exception:

    Name:  Untitled2.jpg
Views: 233
Size:  8.5 KB

    How to solve this?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    #10 is giving this output
    Name:  Untitled2.jpg
Views: 231
Size:  4.4 KB

    can you help me in solving the error of #11?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: DataTable Problem

    vb Code:
    1. Dim counter As Integer = 0
    2. For Each c As DataColumn In dt.Columns
    3.  
    4.     e.Graphics.DrawString(c.ColumnName, _titleFont, Brushes.Black, xPos, yPos)
    5.  
    6.     xPos += myArray(counter)
    7.     counter += 1
    8.  
    9. Next

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    now its working and i am getting a output like this:
    Name:  Untitled2.jpg
Views: 249
Size:  5.9 KB

    can you suggest how more to format the data so that it appears well to the user??

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    Code:
     e.Graphics.DrawLine(Pens.Black, 50, 50, 800, 50)
    
            e.Graphics.DrawLine(Pens.Black, 50, 50, 50, 1050)
    
            e.Graphics.DrawLine(Pens.Black, 50, 1050, 800, 1050)
    
            e.Graphics.DrawLine(Pens.Black, 800, 50, 800, 1050)
    
            e.Graphics.DrawLine(Pens.Black, 100, 110, 750, 110)
    
            e.Graphics.DrawLine(Pens.Black, 100, 150, 750, 150)
    
            e.Graphics.DrawLine(Pens.Black, 100, 1000, 750, 1000)
    but this code is having some problems since if i change the page setup from portrait to landscape then it wont work.......

    even the entire positioning of the contents of the page is changed............

    is there a way to get rid of this problem?

    Then how to do this?
    Last edited by HowTo; Mar 27th, 2010 at 01:30 PM.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    any suggestions/help please?

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    Quote Originally Posted by HowTo View Post
    Code:
     e.Graphics.DrawLine(Pens.Black, 50, 50, 800, 50)
    
            e.Graphics.DrawLine(Pens.Black, 50, 50, 50, 1050)
    
            e.Graphics.DrawLine(Pens.Black, 50, 1050, 800, 1050)
    
            e.Graphics.DrawLine(Pens.Black, 800, 50, 800, 1050)
    
            e.Graphics.DrawLine(Pens.Black, 100, 110, 750, 110)
    
            e.Graphics.DrawLine(Pens.Black, 100, 150, 750, 150)
    
            e.Graphics.DrawLine(Pens.Black, 100, 1000, 750, 1000)
    but this code is having some problems since if i change the page setup from portrait to landscape then it wont work.......

    even the entire positioning of the contents of the page is changed............

    is there a way to get rid of this problem?

    Then how to do this?
    Please help

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataTable Problem

    The problem is that you've written code that only works for one specific paper size and one specific orientation. If you want code that will work with any parameters then you can't hard-code every value. You have to use the information provided by the PrintPage event to determine what the parameters are, e.g. paper size and orientation, and then use those in your code to lay out the printed output accordingly
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    can you show me a sample code to do it....

    last night i were thinking that since i am using the absolute positioning hence i am facing the problem but i cant solve it......

    can you help me with a sample code which i can work on with?

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataTable Problem

    I'd suggest that you take a look at Merrion's CodeBank thread that provides DataGridPrinter and DataGridViewPrinter classes. I'm sure it handles different page orientations and more. Note that that is a different thread to the printing intro.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataTable Problem

    ok then i am creating a new thread on this topic.....
    can you tell how can i bring merrions attraction towards this thread?I cant find him online and i have got lots and lots of questions to ask him about this printing issue.......
    Please

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