14 Replies - 2158 Views - Last Post: 27 December 2012 - 01:54 AM Rate Topic: -----

#1 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Print data on a paper sheet

Posted 16 December 2012 - 07:48 AM

Hi,

First of all sorry for not providing the code ,because I have no Idea where to start from,
here is the example of what I have in mind.

COMPANY NAME            date
============            =====
SL   DESIGNATION     NAME     SALARY   LEAVES EARNED
==   =====           =====    =======   ============
1    DESIGNATION      NAME1       1         2
2    DESIGNATION      NAME2       1         4
3    DESIGNATION      NAME3       1         6
4    DESIGNATION      NAME4       1         5


With values from database .

This post has been edited by chdboy: 16 December 2012 - 07:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Print data on a paper sheet

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Print data on a paper sheet

Posted 16 December 2012 - 08:10 AM

View Postchdboy, on 16 December 2012 - 08:48 AM, said:

First of all sorry for not providing the code ,because I have no Idea where to start from,
here is the example of what I have in mind.
...
With values from database .

What do you have so far? If you have a database and code to access it, and the ability to see it on your form, you have something.

The problem is, we have no clue as to what that something is.

So about the only thing we can tell you is to make strings of the data you want to print, put them together into a document, and print it. If that sounds vague, it's because the info you gave us is vague.
Was This Post Helpful? 0
  • +
  • -

#3 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Print data on a paper sheet

Posted 17 December 2012 - 04:46 AM

I have vague information because as I told earlier,that I don't know where to start .
Was This Post Helpful? 0
  • +
  • -

#4 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 17 December 2012 - 04:54 AM

i will suggest you to first create a datagridview ,,,enter your data in grid,,after that try printing your grid that will be easy
Was This Post Helpful? 0
  • +
  • -

#5 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 17 December 2012 - 05:09 AM

this is the way to print your datagrid....
firslt create a new form say form2... take a richtextbox
  Public Sub Print1()
        TxtFile = "C:\Temp\123.rft"
        sw = File.CreateText(TxtFile)
 Dim i As Integer = DataGridView1.RowCount
        MsgBox(i)
        For i = 0 To DataGridView1.RowCount - 1
            sw.WriteLine(DataGridView1.Rows(i).Cells(0).Value & "      " & DataGridView1.Rows(i).Cells(1).Value & _
                         "      " & DataGridView1.Rows(i).Cells(2).Value & "      " & DataGridView1.Rows(i).Cells(3).Value & _
                         "      " & DataGridView1.Rows(i).Cells(4).Value & "      " & DataGridView1.Rows(i).Cells(5).Value & _
                         "      " & DataGridView1.Rows(i).Cells(6).Value)
           

        Next
       
        sw.Close()
        Form2.rt1.LoadFile(TxtFile, RichTextBoxStreamType.PlainText)

        Form2.Show()



it will not display the records in a proper format(irregular format) so you have to work for the page setting,,,for e.g margin,page size and so on...

This post has been edited by kai_itz me: 17 December 2012 - 05:11 AM

Was This Post Helpful? 1
  • +
  • -

#6 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Print data on a paper sheet

Posted 18 December 2012 - 07:40 AM

View Postkai_itz me, on 17 December 2012 - 05:09 AM, said:

this is the way to print your datagrid....
firslt create a new form say form2... take a richtextbox
  Public Sub Print1()
        TxtFile = "C:\Temp\123.rft"
        sw = File.CreateText(TxtFile)
 Dim i As Integer = DataGridView1.RowCount
        MsgBox(i)
        For i = 0 To DataGridView1.RowCount - 1
            sw.WriteLine(DataGridView1.Rows(i).Cells(0).Value & "      " & DataGridView1.Rows(i).Cells(1).Value & _
                         "      " & DataGridView1.Rows(i).Cells(2).Value & "      " & DataGridView1.Rows(i).Cells(3).Value & _
                         "      " & DataGridView1.Rows(i).Cells(4).Value & "      " & DataGridView1.Rows(i).Cells(5).Value & _
                         "      " & DataGridView1.Rows(i).Cells(6).Value)
           

        Next
       
        sw.Close()
        Form2.rt1.LoadFile(TxtFile, RichTextBoxStreamType.PlainText)

        Form2.Show()



it will not display the records in a proper format(irregular format) so you have to work for the page setting,,,for e.g margin,page size and so on...

Thank you very much ,now I have something to work on.
Now I think I have to make a print method ,and then load it in the printer with page settings and such .
so it can take a command to print.

I hope this is all I need .


Thanks again for the code.
Was This Post Helpful? 0
  • +
  • -

#7 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 18 December 2012 - 11:51 PM

no problem at all.. gud luck
Was This Post Helpful? 0
  • +
  • -

#8 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Print data on a paper sheet

Posted 20 December 2012 - 09:06 PM

But when I tried to display the header with this code
sw.WriteLine(DataGridView1.Rows(0).HeaderCell.Value)

The header value does not appear ?
Was This Post Helpful? 0
  • +
  • -

#9 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 21 December 2012 - 12:19 AM

SORRY FOR LATE REPLY DUDE
actually i was too working over the same topic..

here is what i have got.

use the below code,it will automatically create a file(txt) in c drive with your complete requirement...BUT U STILL HAVE TO MANAGE YOUR PAGE STTING

 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim numCols As Integer = DataGridView.ColumnCount
        Dim numRows As Integer = DataGridView.RowCount - 1
        Dim strDestinationFile As String = "c:\\output.txt"
        Dim tw As TextWriter = New StreamWriter(strDestinationFile)

        'writing the header
        For count As Integer = 0 To numCols - 1
            tw.Write(DataGridView.Columns(count).HeaderText)
            If (count <> numCols - 1) Then
                tw.Write(", ")
            End If
        Next
        tw.WriteLine()

        For count As Integer = 0 To numRows - 1
            For count2 As Integer = 0 To numCols - 1
                tw.Write(DataGridView.Rows(count).Cells(count2).Value)
                If (count2 <> numCols) Then
                    tw.Write(", ")
                End If
            Next
            tw.WriteLine()
        Next
        tw.Close()
    End Sub



just put the above code in a button_click event as you can see,,,
HAVE FUN :smartass:/>

This post has been edited by kai_itz me: 21 December 2012 - 12:22 AM

Was This Post Helpful? 0
  • +
  • -

#10 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 21 December 2012 - 03:24 AM

OR YOU CAN USE THIS ONE.. BOTH ARE TESTED

 Dim writer As StreamWriter = New StreamWriter("C:\Temp\123.rft")

        If (DataGridView1.Rows.Count - 1) Then
            For Each col As DataGridViewColumn In
            DataGridView1.Columns
                If (col.Index = (DataGridView1.Columns.Count - 1)) Then

                    writer.WriteLine(col.HeaderText)
                Else
                    writer.Write(String.Concat(col.HeaderText, ","))
                End If
            Next
            For Each row As DataGridViewRow In DataGridView1.Rows
                'If Not omitIndices.Contains(row.Index) Then
                For Each cell As DataGridViewCell In row.Cells
                    If (cell.OwningColumn.Index _
                    = (DataGridView1.Columns.Count - 1)) Then

                        If (Not (cell.Value) Is Nothing) Then
                            writer.WriteLine(cell.Value.ToString)
                        Else
                            writer.WriteLine("")
                        End If
                    ElseIf (Not (cell.Value) Is Nothing) Then

                        writer.Write(String.Concat(cell.Value.ToString, ","))
                    Else
                        writer.Write(String.Concat("", ","))
                    End If
                Next
                'End If
            Next
        End If
        writer.Close()



PUT THIS IN A CLICK EVENT.........
Was This Post Helpful? 0
  • +
  • -

#11 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Print data on a paper sheet

Posted 23 December 2012 - 01:18 AM

Nothing happened with the above two code ,No errors,Nothing.

here is the full button click code

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim writer As StreamWriter = New StreamWriter("C:\Temp\123.rft")

        If (DataGridView1.Rows.Count - 1) Then
            For Each col As DataGridViewColumn In
            DataGridView1.Columns
                If (col.Index = (DataGridView1.Columns.Count - 1)) Then

                    writer.WriteLine(col.HeaderText)
                Else
                    writer.Write(String.Concat(col.HeaderText, ","))
                End If
            Next
            For Each row As DataGridViewRow In DataGridView1.Rows
                'If Not omitIndices.Contains(row.Index) Then
                For Each cell As DataGridViewCell In row.Cells
                    If (cell.OwningColumn.Index _
                    = (DataGridView1.Columns.Count - 1)) Then

                        If (Not (cell.Value) Is Nothing) Then
                            writer.WriteLine(cell.Value.ToString)
                        Else
                            writer.WriteLine("")
                        End If
                    ElseIf (Not (cell.Value) Is Nothing) Then

                        writer.Write(String.Concat(cell.Value.ToString, ","))
                    Else
                        writer.Write(String.Concat("", ","))
                    End If
                Next
                'End If
            Next
        End If
        writer.Close()
End Sub

Was This Post Helpful? 0
  • +
  • -

#12 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 23 December 2012 - 09:39 PM

you dont get it...there is already a file created in c:\temp with name 123.

okay , LET ME EXPLAIN

1)create two form say form1 and form2.
2)create a button on form 1.
3)take a richtextbox over form2.

now inside your button_click event put this code.

 TxtFile = "C:\Temp\OUTPUT.txt"

        Dim writer As StreamWriter = New StreamWriter("C:\Temp\123.rft")
        writer = File.CreateText(TxtFile)
        If (DataGridView1.Rows.Count - 1) Then
            For Each col As DataGridViewColumn In
            DataGridView1.Columns
                If (col.Index = (DataGridView1.Columns.Count - 1)) Then

                    writer.WriteLine(col.HeaderText)
                Else
                    writer.Write(String.Concat(col.HeaderText, ","))
                End If
            Next
            For Each row As DataGridViewRow In DataGridView1.Rows
                'If Not omitIndices.Contains(row.Index) Then
                For Each cell As DataGridViewCell In row.Cells
                    If (cell.OwningColumn.Index _
                    = (DataGridView1.Columns.Count - 1)) Then

                        If (Not (cell.Value) Is Nothing) Then
                            writer.WriteLine(cell.Value.ToString)
                        Else
                            writer.WriteLine("")
                        End If
                    ElseIf (Not (cell.Value) Is Nothing) Then

                        writer.Write(String.Concat(cell.Value.ToString, ","))
                    Else
                        writer.Write(String.Concat("", ","))
                    End If
                Next
                'End If
            Next
        End If
        writer.Close()
        
     ' THESE LINES WILL SHOW UR RESULT ON RICHTEXTBOX  

        Form2.rt1.LoadFile(TxtFile, RichTextBoxStreamType.PlainText)
        Form2.Show()



YOU WILL GET THE RSULT OVER RICHTEXTFORM AS WELL AS THERE IS ALSO A FILE CREATED IN C DRIVE WITH OUTPUT AS NAME. STILL IF GOT PROBLEM LET ME KNOW

This post has been edited by kai_itz me: 23 December 2012 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#13 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 24 December 2012 - 12:28 AM

you dont get it...there is already a file created in c:\temp with name 123.

YET THERE WAS SOME PROBLEM IN PREVIOUS CODE, TRY THIS..

LET ME EXPLAIN STEP BY STEP,,,,,

1)create two form say form1 and form2.
2)create a button on form 1.
3)take a richtextbox over form2.

now inside your button_click event put this code.

 Dim numCols As Integer = DataGridView.ColumnCount
        Dim numRows As Integer = DataGridView.RowCount - 1
        Dim strDestinationFile As String = "c:\\output.txt"

        Dim writer As TextWriter = New StreamWriter(strDestinationFile)

        'writing the header
        For count As Integer = 0 To numCols - 1
            writer.Write(DataGridView.Columns(count).HeaderText)
            If (count <> numCols - 1) Then
                writer.Write(", ")
            End If
        Next
        writer.WriteLine()

        For count As Integer = 0 To numRows - 1
            For count2 As Integer = 0 To numCols - 1
                writer.Write(DataGridView.Rows(count).Cells(count2).Value)
                If (count2 <> numCols) Then
                    writer.Write(", ")
                End If
            Next
            writer.WriteLine()
        Next
        writer.Close()

' THESE LINES WILL SHOW UR RESULT ON RICHTEXTBOX 
  
        Form2.RichTextBox1.LoadFile("c:\\output.txt", RichTextBoxStreamType.PlainText)
        Form2.Show()




YOU WILL GET THE RSULT OVER RICHTEXTBOX AS WELL AS THERE IS ALSO A FILE CREATED IN C DRIVE WITH OUTPUT AS NAME. STILL IF U GOT any PROBLEM LET ME KNOW...

This post has been edited by kai_itz me: 24 December 2012 - 12:33 AM

Was This Post Helpful? 1
  • +
  • -

#14 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Print data on a paper sheet

Posted 26 December 2012 - 11:25 PM

Now my Code looks like this .I have added a few lines into it ...as code given by you just display results into a RTB.


 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click




        Dim TxtFile = "C:\Temp\OUTPUT.txt"

        Dim writer As StreamWriter = New StreamWriter("C:\Temp\123.rft")
        writer = File.CreateText(TxtFile)
        If (DGV1.Rows.Count - 1) Then
            For Each col As DataGridViewColumn In
            DGV1.Columns
                If (col.Index = (DGV1.Columns.Count - 1)) Then

                    writer.WriteLine(col.HeaderText)
                Else
                    writer.Write(String.Concat(col.HeaderText, ","))
                End If
            Next
            For Each row As DataGridViewRow In DGV1.Rows
                'If Not omitIndices.Contains(row.Index) Then
                For Each cell As DataGridViewCell In row.Cells
                    If (cell.OwningColumn.Index _
                    = (DGV1.Columns.Count - 1)) Then

                        If (Not (cell.Value) Is Nothing) Then
                            writer.WriteLine(cell.Value.ToString)
                        Else
                            writer.WriteLine("")
                        End If
                    ElseIf (Not (cell.Value) Is Nothing) Then

                        writer.Write(String.Concat(cell.Value.ToString, ","))
                    Else
                        writer.Write(String.Concat("", ","))
                    End If
                Next
                'End If
            Next
        End If
        writer.Close()

        ' THESE LINES WILL SHOW UR RESULT ON RICHTEXTBOX 

        Print.RichTextBox1.LoadFile(TxtFile, RichTextBoxStreamType.PlainText)
        Print.Show()
        Dim proc As New Process
        proc.StartInfo.FileName = "C:\Temp\OUTPUT.txt"
        proc.StartInfo.Verb = "Print"
        proc.StartInfo.CreateNoWindow = True
        proc.Start()

    End Sub

Was This Post Helpful? 0
  • +
  • -

#15 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Print data on a paper sheet

Posted 27 December 2012 - 01:54 AM

USE THIS ONE INSTEAD OF WHATEVER U ARE TRYING....
THERE WAS SOME PROBLEM IN PREVIOUS CODE, TRY THIS..

Quote

LET ME EXPLAIN STEP BY STEP,,,,,

1)create two form say form1 and form2.
2)create a button on form 1.
3)take a richtextbox over form2.

now inside your button_click event put this code.

view sourceprint?
01 Dim numCols As Integer = DataGridView.ColumnCount

02 Dim numRows As Integer = DataGridView.RowCount - 1

03 Dim strDestinationFile As String = "c:\\output.txt"

04

05 Dim writer As TextWriter = New StreamWriter(strDestinationFile)

06

07 'writing the header

08 For count As Integer = 0 To numCols - 1

09 writer.Write(DataGridView.Columns(count).HeaderText)

10 If (count <> numCols - 1) Then

11 writer.Write(", ")

12 End If

13 Next

14 writer.WriteLine()

15

16 For count As Integer = 0 To numRows - 1

17 For count2 As Integer = 0 To numCols - 1

18 writer.Write(DataGridView.Rows(count).Cells(count2).Value)

19 If (count2 <> numCols) Then

20 writer.Write(", ")

21 End If

22 Next

23 writer.WriteLine()

24 Next

25 writer.Close()

26

27 ' THESE LINES WILL SHOW UR RESULT ON RICHTEXTBOX

28

29 Form2.RichTextBox1.LoadFile("c:\\output.txt", RichTextBoxStreamType.PlainText)

30 Form2.Show()



YOU WILL GET THE RSULT OVER RICHTEXTBOX AS WELL AS THERE IS ALSO A FILE CREATED IN C DRIVE WITH OUTPUT AS NAME. STILL IF U GOT any PROBLEM LET ME KNOW...

This post has been edited by kai_itz me: 27 December 2012 - 01:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1