|
-
November 12th, 2006, 04:11 PM
#1
format text for printing
I'm planning to print the contents of a csv text file
The data is in a number of rows and columns and I've put the whole lot into a 2 dimensional array, and found out which are the widest columns.
Now I want to print my array.
I need to left align text and right align numbers. I can work out which one I need using isNumeric().
How can I get my columns to align how I want them to?
H
I'm using .NET Framework 3.5
I'm planning to be spontaneous tomorrow
-
November 12th, 2006, 06:30 PM
#2
Re: format text for printing
Do you know anything about printing in .NET? Everything goes exactly where you tell it to. You measure your strings to determine how wide the columns are then you call DrawString to print it exactly where you want. You can specify alignment when calling DrawString. I suggest that you take a look at the printing example in the 2003 101 samples from Microsoft and once you have the basics post back with specific questions about the bits that are giving you grief.
-
November 13th, 2006, 12:25 AM
#3
Re: format text for printing
You can get the contents of cxv file into a dataTable.
Code:
Dim dt2 As New DataTable("tb2")
Code:
Dim oCon As Odbc.OdbcConnection
Dim oDA As Odbc.OdbcDataAdapter
Dim sConString As String
Dim sFolder As String = "C:\"
Dim sFile As String = "test1.csv"
sConString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & sFolder & ";"
oCon = New Odbc.OdbcConnection(sConString)
oDA = New Odbc.OdbcDataAdapter("Select * From [" + sFile + "]", oCon)
oDA.Fill(dt2)
DataGridView1.DataSource = dt2.DefaultView
then Print the dataTable
-
November 13th, 2006, 12:32 AM
#4
Re: format text for printing
Sample prints datatable with 3 columns.
Delcaration
Code:
Dim record As Integer 'row counter
Print Button
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
printDialog1 = New PrintDialog
pDoc = New PrintDocument()
printDialog1.Document = pDoc
If printDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
pDoc.Print()
End If
End Sub
Code:
Private Sub mdoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pDoc.BeginPrint
record = 0
End Sub
Code:
Private Sub mdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pDoc.PrintPage
'line counter
Dim count As Integer = 0
'set margins
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
'font first set bold for column names
Dim printFont As Font = New Font("Times New Roman", 12, FontStyle.Bold)
'xposition for column change
'yposition for line change
Dim yPosition As Single = (topMargin + (count * printFont.GetHeight(e.Graphics))) 'initial position
Dim xposition As Single = e.MarginBounds.Width / dt.Columns.Count
'draw columnNames
For i As Integer = 0 To dt.Columns.Count - 1
e.Graphics.DrawString(dt.Columns.Item(i).ToString, printFont, Brushes.Black, leftMargin, yPosition, New StringFormat())
leftMargin += xposition
Next
count += 1
yPosition = (topMargin + (count * printFont.GetHeight(e.Graphics)))
leftMargin = e.MarginBounds.Left
'draw separating line
e.Graphics.DrawLine(Pens.Black, leftMargin, yPosition, e.MarginBounds.Width, yPosition)
count += 1
printFont = New Font("Times New Roman", 12)
Dim linesPerPage As Single = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
While count < linesPerPage And record < dt.Rows.Count
yPosition = (topMargin + (count * printFont.GetHeight(e.Graphics)))
'draw Row from datatable
For i As Integer = 0 To dt.Columns.Count - 1
e.Graphics.DrawString(dt.Rows(record).Item(i).ToString, printFont, Brushes.Black, leftMargin, yPosition, New StringFormat())
leftMargin += xposition
Next
leftMargin = e.MarginBounds.Left
count += 1
record += 1
End While
If record < dt.Rows.Count Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
-
November 13th, 2006, 02:12 PM
#5
Re: format text for printing
Thank you aniskhan for coming to my aid again. Unfortunately I've falled at the first hurdle as I don't have the System.Data.Odbc namespace which I suspect I need. I'm using .net version 1.0.
I think I can create a datatable and fill it from the 2 dimensional array I've created using the data from my csv file instead, a less elegant solution, but a workable one!
I can then adapt your excellent printing code to print my datatable and my problem is solved.
I'm using .NET Framework 3.5
I'm planning to be spontaneous tomorrow
-
November 13th, 2006, 04:31 PM
#6
Re: format text for printing
u can also use oledb
Code:
Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties=Text;"
Dim Conn As New OleDb.OleDbConnection(strconn)
Dim adapter As New OleDb.OleDbDataAdapter("SELECT * FROM a1.csv", Conn)
Dim dt As New DataTable("TableName")
Conn.Open()
adapter.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
Conn.Close()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|