printing a datagrid in VB2005 (Again)

Has anybody found a good solution to this yet

I can not get my grid to print I can only see some "C" code but that has not helped me much.



Answer this question

printing a datagrid in VB2005 (Again)

  • B-Dubs

    Dear Scott,

     

    I am using VB2k5 recently. My doubt is= I have a GRID with contents. I want to print those data to my printer. WHat are the steps I need to do to reach my desire

    DO you have any manual or technique sorry english bad too.

    thanks, Bruno.


  • John.NET

    Scott

    Thanks that method is pretty easy as I have text print program working well nearly, i just did as you said and it works.

    One problem the datagrid is longer than the screen so it has a scroll bar on the screen whith the method you suggest I only print on the printer whats on the screen including the scrollbar. I need to figure out how to print the values and TAB them accross the screen I do not need all the lines.

    The Grid only containes text.

    Thanks Steve


  • UKMike

    By print, you mean to a printer, right

    First, you'd get a bitmap that shows what's on the control, using Control.DrawToBitmap (air code -- something like this):

           Dim rect As Rectangle = myGrid.ClientRectangle
           Dim bmp As new Bitmap(rect.Width, rect.Height)
           myGrid.DrawToBitmap(bmp, rect)

    Then you'd do the actual printing.  There's info on printing at http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsPrinting.aspx (it's not as complicated as it first appears).  Where they're using Graphics.DrawString in a loop, you'll use Graphics.DrawImage, with your bitmap being the image.

                   -Scott


  • Antonio72

    That would put you at code that's a little closer to the printing sample: you'd print the values out yourself.  You wouldn't use DrawToBitmap anymore, and you'd use Graphics.DrawString instead of drawing the image.  DrawString lets you specify exactly where you want the string to print, so you'll get pretty fine control over positioning (it wouldn't really be tabbing, but you can make it have the same effect).

        -Scott


  • DGMoffat

    Help,

    I want to print data from a Data Grid in vb2006, I tried the above code and it didnt work.

    My DataGrid property list uses.

    .VisibleRowCount

    &

    .VisibleColumnCount

    So I changed them, it also uses.

    .Row

    Instead of

    .Rows

    Im Getting a compile error "Wrong number of arguments or Invaled Property assignment", on line

    MSDataGrid1.CurentCell = MSDataGrid1.Row(msfr).Cells(msfc) 'Its coming back with Row Highlighted

    Sample Data Grid View:

    Date MornIn MornOut AftIn AftOut TotalHrs

    7/24 8:00 12:00 12:30 4:00 8.0

    7/25 8:00 ect........................

    7/26 ect...................So I have a Known Number of Columns but varying number of rows.

    Can Anyone help me out


  • TRW_Mike

    Scott thanks for your help it works fine now here is the code i used

     

    'code to print grid data to printer

    Dim msfc As Integer 'cols

    Dim msfr As Integer 'rows

    printy = marginTop + 30

    printx = marginLeft

    For msfr = 0 To MSFlexGrid1.RowCount - 1

    For msfc = 0 To MSFlexGrid1.ColumnCount - 1

    If msfc = 0 Then printx = marginLeft

    If msfc = 1 Then printx = marginLeft + 300

    If msfc = 2 Then printx = marginLeft + 420

    MSFlexGrid1.CurrentCell = MSFlexGrid1.Rows(msfr).Cells(msfc)

    temp = MSFlexGrid1.CurrentCell.Value

    e.Graphics.DrawString(temp, font, Brushes.Black, printx, printy, fmt)

    printx += +160

    Next

    printy += +20

    Next


  • printing a datagrid in VB2005 (Again)