|
-
Jul 19th, 2009, 09:05 AM
#41
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
truely speaking sir,
I referred a few books yesterday.........but i found all of them confusing.......
You just help me step by step sir....I might complete my work this whole night..............
I have got the confidence sir........all i need is your help..........
-
Jul 19th, 2009, 09:09 AM
#42
Re: Printing problem........Need some help
That said... if you really want to try it I'd make a backup copy of your code as it is, and then take a look at this.
Personally I'd probably just construct an HTML page with your table and load that up into the user's default browser to printout.
-
Jul 19th, 2009, 09:11 AM
#43
Re: Printing problem........Need some help
 Originally Posted by gautamshaw
You just help me step by step sir....I might complete my work this whole night..............
I have got the confidence sir........all i need is your help..........
While I'm flattered that you have the confidence, unfortunately I cant sit here all day helping you out line by line (England are beating Australia in the cricket right now, and it's the final round of the Open golf - both of which are competing for my attention!)
-
Jul 19th, 2009, 09:12 AM
#44
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
i am ready sir..........I just need your guidance........
Please help me sir.....
-
Jul 19th, 2009, 09:14 AM
#45
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
I also love watching the ashes Sir...........
Dont you ve any code..........Which you can give to me.....
-
Jul 19th, 2009, 09:17 AM
#46
Re: Printing problem........Need some help
My honest opinion - take a look at the VB code in the example I linked to - you will need to modify it slightly to include your watermark, but that is probably the simplest way of doing it.
Note though that i haven't used that source code at all so I can't necessarily give you any great help with it.
I have probably got some example code of how to take a gridview and build the contents into an HTML table but its on my old laptop - I will see if I can dig it out.
-
Jul 19th, 2009, 09:21 AM
#47
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
ok sir then you just give me the link where i could find it........
-
Jul 19th, 2009, 09:32 AM
#48
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
One last confusion sir regarding the excel printing........
I tried to make the cells dynamically depending on the number of rows and columns present.......
Code:
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
With xlWorkSheet
' xlApp.Range("A10:C13").Borders.LineStyle = 1
'xlApp.Range("A10:C13").HorizontalAlignment = 2
xlApp.Range("A10:C10").Font.Bold = 15
.Cells(10, 1).Value = "Name"
.Cells(10, 2).Value = "Address"
.Cells(10, 3).Value = "Roll"
End With
For i = 1 To DataGridView1.RowCount - 1
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 10, j + 1) = DataGridView1(j, i - 1).Value.ToString()
Next
Next
xlApp.Range("A10:C" & i).Borders.LineStyle = 1
xlApp.Range("A10:C" & i).HorizontalAlignment = 2
xlWorkSheet.Shapes.AddTextEffect(PresetTextEffect:=3, _
Text:="Goutam", FontName:="Arial Green", FontSize:=50, _
FontBold:=False, FontItalic:=False, Left:=150, Top:=0).Select()
'xlApp.Range("A1:C" & i).Font.Bold = 15
xlWorkSheet.PrintOut()
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
I get bad output.....
Attachment 72079
Please dont get irritated sir......I need your help
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 19th, 2009, 09:36 AM
#49
Re: Printing problem........Need some help
You need to change this
Code:
xlApp.Range("A10:C" & i).Borders.LineStyle = 1
xlApp.Range("A10:C" & i).HorizontalAlignment = 2
because i is the number of rows in your grid, which in your example is 3, so the range you are looking at is A10:C3. You want it to go from A10 to C13, so it should be
Code:
xlApp.Range("A10:C" & 10+i).Borders.LineStyle = 1
xlApp.Range("A10:C" & 10+ i).HorizontalAlignment = 2
-
Jul 19th, 2009, 09:40 AM
#50
Re: Printing problem........Need some help
Right... I've dug out my old code which takes a DataGridView control and creates a HTML document containing the table which can then be viewed and printed by your default web browser.
This is a stripped down version as the original had code for emulating formatting (like different coloured backgrounds in cells etc), but it should do what you want it to. You need to call this routine passing in your datagridview, and retrieve the filename returned and then fire the webbrowser to open the file.
Code:
Private Function RenderListAsHTML(ByRef DataGrid As DataGridView) As String
Dim swrOutput As StreamWriter
Dim sFormat As String
Dim sPath As String
'establish a temporary filename
sPath = Path.GetTempFileName
swrOutput = New StreamWriter(sPath)
swrOutput.WriteLine("<head>")
swrOutput.WriteLine("<title>Your Title Here</title>")
swrOutput.WriteLine("</head>")
swrOutput.WriteLine("<style type='text/css'>")
swrOutput.WriteLine("th")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("tr")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("</style>")
swrOutput.WriteLine("<body>")
swrOutput.WriteLine("<table>")
'column headers
For Each dvgHeader As DataGridViewColumn In DataGrid.Columns
swrOutput.WriteLine("<th bgcolor='silver'>" & dvgHeader.HeaderText & "</th>")
Next
'actual data
For Each dvgRow As DataGridViewRow In DataGrid.Rows
If dvgRow.Visible Then
swrOutput.Write("<tr>")
For Each objCell As DataGridViewCell In dvgRow.Cells
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
Next
swrOutput.Write("</tr>")
End If
Next
swrOutput.WriteLine("</table>")
swrOutput.WriteLine("</body>")
swrOutput.Flush()
swrOutput.Close()
Return sPath
End Function
-
Jul 19th, 2009, 09:48 AM
#51
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
I am trying it out sir.......
You hust try to be online for a few minutes........
Its a request sir.........
I will reply soon after trying it.....
give me only 5 to 7 minutes...........
Please remain online sir for helping me........
-
Jul 19th, 2009, 09:50 AM
#52
Re: Printing problem........Need some help
NB - in order to incorporate your watermark you'll need to have a prepared image, and use
Code:
swrOutput.WriteLine("<table style='background-image: url(""your_image.gif"")'>"
-
Jul 19th, 2009, 10:00 AM
#53
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
i am getting an error in this line sir......
Code:
Dim swrOutput As StreamWriter
It is telling that :
StreamWriter not defined.............
where and how to define it sir?
-
Jul 19th, 2009, 10:01 AM
#54
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
I think i need to add reference..........am i right sir?
-
Jul 19th, 2009, 10:02 AM
#55
Re: Printing problem........Need some help
You'll need to either add a line at the top of your form saying "Imports System.IO" or change the line to
Code:
Dim swrOutput as System.IO.StreamWriter
-
Jul 19th, 2009, 10:05 AM
#56
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
one more error sir......
Code:
Dim sFormat As String
unused local variable:"sFormat"
Should I delete this line sir?
what to do?
-
Jul 19th, 2009, 10:08 AM
#57
Re: Printing problem........Need some help
Ah yeah - it should be a warning not an error (ie it won't stop your code from running), but you can just delete that line - it is a hangover from the code I removed handling specific formatting for rows.
-
Jul 19th, 2009, 10:11 AM
#58
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
You need to call this routine passing in your datagridview, and retrieve the filename returned and then fire the webbrowser to open the file.
should i call this function ?
Code:
Private Function RenderListAsHTML(ByRef DataGrid As DataGridView) As String
in which event of gridview should i call it?
My head is totally gone sir.........I am very tensed still..please help me sir.....
-
Jul 19th, 2009, 10:17 AM
#59
Re: Printing problem........Need some help
I presume you have a Print button on your form that calls your print routine... button3 by the looks of things.
In that button's click event you would call this routine. Note that you need to pass in the name of your gridview, and it will return the filename of the file that it has created.
From there you can either launch the user's default browser using process.start, or you can create a new webbrowser object and load the HTML file into it and print automatically if thats what you want.
-
Jul 19th, 2009, 10:21 AM
#60
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.RenderListAsHTML(DataGridView1)
End Sub
I did this sir......
-
Jul 19th, 2009, 10:23 AM
#61
Re: Printing problem........Need some help
OK... but all that will do is create the HTML into a file, and as you aren't retrieving the returned value you aren't going to be able to do anything more.
How do you normally retrieve the return value from a function?
-
Jul 19th, 2009, 10:25 AM
#62
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
then what to do sir.......
Here is my code:
Code:
Imports System.Data
Imports System.Data.OleDb
Imports system.io
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste1\waste1\db1.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
Dim ds As DataSet = New DataSet
DataGridView1.Columns(0).DataPropertyName = "Name"
DataGridView1.Columns(1).DataPropertyName = "Address"
DataGridView1.Columns(2).DataPropertyName = "Roll"
da.Fill(ds, "Table1")
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Function RenderListAsHTML(ByRef DataGrid As DataGridView) As String
Dim swrOutput As StreamWriter
' Dim sFormat As String
Dim sPath As String
'establish a temporary filename
sPath = Path.GetTempFileName
swrOutput = New StreamWriter(sPath)
swrOutput.WriteLine("<head>")
swrOutput.WriteLine("<title>Your Title Here</title>")
swrOutput.WriteLine("</head>")
swrOutput.WriteLine("<style type='text/css'>")
swrOutput.WriteLine("th")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("tr")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("</style>")
swrOutput.WriteLine("<body>")
swrOutput.WriteLine("<table>")
'column headers
For Each dvgHeader As DataGridViewColumn In DataGrid.Columns
swrOutput.WriteLine("<th bgcolor='silver'>" & dvgHeader.HeaderText & "</th>")
Next
'actual data
For Each dvgRow As DataGridViewRow In DataGrid.Rows
If dvgRow.Visible Then
swrOutput.Write("<tr>")
For Each objCell As DataGridViewCell In dvgRow.Cells
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
Next
swrOutput.Write("</tr>")
End If
Next
swrOutput.WriteLine("</table>")
swrOutput.WriteLine("</body>")
swrOutput.Flush()
swrOutput.Close()
Return sPath
End Function
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.RenderListAsHTML(DataGridView1)
End Sub
End Class
When i run the code an exception is thrown......
Attachment 72080
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 19th, 2009, 10:26 AM
#63
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
-
Jul 19th, 2009, 10:31 AM
#64
Re: Printing problem........Need some help
Are there any empty cells in the gridview? If so you may need to trap that.
Try putting a breakpoint on the first "For Each" line and step through the code and see if the error occurs on the first cell or if it is part way through the grid (ie does it get as far as the first "Next").
-
Jul 19th, 2009, 10:37 AM
#65
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
If dvgRow.Visible Then
swrOutput.Write("<tr>")
For Each objCell As DataGridViewCell In dvgRow.Cells
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
Next
swrOutput.Write("</tr>")
End If
Next
In the above code sir when the loop goes to this line for the thied time:
Code:
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
then the exception is thrown..........
My design is like this:
Attachment 72081
How to correct this exception sir?
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 19th, 2009, 10:43 AM
#66
Re: Printing problem........Need some help
OK it looks like it is treating the "New Entry" row as an actual row, which I didn't realise it would do.
You can get around it by changing the code like this :
Code:
If dvgRow.Visible And Not dvgRow.IsNewRow Then
swrOutput.Write("<tr>")
For Each objCell As DataGridViewCell In dvgRow.Cells
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
Next
swrOutput.Write("</tr>")
End If
-
Jul 19th, 2009, 10:51 AM
#67
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
When i click the print button...no error......no output also...
Do i need to add reference to render html?
look at this sir....
Attachment 72083
Here is my full code:
Code:
Imports System.Data
Imports System.Data.OleDb
Imports system.io
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste1\waste1\db1.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
Dim ds As DataSet = New DataSet
DataGridView1.Columns(0).DataPropertyName = "Name"
DataGridView1.Columns(1).DataPropertyName = "Address"
DataGridView1.Columns(2).DataPropertyName = "Roll"
da.Fill(ds, "Table1")
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Function RenderListAsHTML(ByRef DataGrid As DataGridView) As String
Dim swrOutput As StreamWriter
' Dim sFormat As String
Dim sPath As String
'establish a temporary filename
sPath = Path.GetTempFileName
swrOutput = New StreamWriter(sPath)
swrOutput.WriteLine("<head>")
swrOutput.WriteLine("<title>Your Title Here</title>")
swrOutput.WriteLine("</head>")
swrOutput.WriteLine("<style type='text/css'>")
swrOutput.WriteLine("th")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("tr")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("</style>")
swrOutput.WriteLine("<body>")
swrOutput.WriteLine("<table>")
'column headers
For Each dvgHeader As DataGridViewColumn In DataGrid.Columns
swrOutput.WriteLine("<th bgcolor='silver'>" & dvgHeader.HeaderText & "</th>")
Next
'actual data
For Each dvgRow As DataGridViewRow In DataGrid.Rows
If dvgRow.Visible And Not dvgRow.IsNewRow Then
swrOutput.Write("<tr>")
For Each objCell As DataGridViewCell In dvgRow.Cells
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
Next
swrOutput.Write("</tr>")
End If
Next
swrOutput.WriteLine("</table>")
swrOutput.WriteLine("</body>")
swrOutput.Flush()
swrOutput.Close()
Return sPath
End Function
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.RenderListAsHTML(DataGridView1)
End Sub
End Class
What to do sir........
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 19th, 2009, 10:53 AM
#68
Re: Printing problem........Need some help
You need to go back and look at posts #59 and #61 where I explained that you now need to open up the file that you've created.
-
Jul 19th, 2009, 10:56 AM
#69
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
I cant follow you sir....
I have not created any file externally sir.........
Is the file automatically created when i run this code somewhere?
I cant get your point sir......
I am sorry again......
-
Jul 19th, 2009, 10:59 AM
#70
Re: Printing problem........Need some help
That whole procedure I gave you creates a file, and it returns the name of the file it has created.
-
Jul 19th, 2009, 11:02 AM
#71
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Where should i get the file name and the file sir?
I have not given any path in my code sir..........
Then how to get the file?
I cant get ur point sir...............
-
Jul 19th, 2009, 11:05 AM
#72
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
should i need to do some more coding sir?
please help me sir!!!!!!!
-
Jul 19th, 2009, 11:06 AM
#73
Re: Printing problem........Need some help
Do you understand any of the code?
This
Code:
'establish a temporary filename
sPath = Path.GetTempFileName
Gets a temporary filename (as it says in the comment) and this
returns the filename it has used to the calling function.
I'm sure you know that to get a return value from a function you assign the result to a variable, for example :
Code:
Dim myValue as integer = DoSomeCalculation(1, 2)
So in order to get (in your button_click event) the name of the file that was created by the RenderListasHTML function, you need to do something similar...
-
Jul 19th, 2009, 11:07 AM
#74
Re: Printing problem........Need some help
Last edited by keystone_paul; Jul 19th, 2009 at 11:19 AM.
-
Jul 19th, 2009, 11:12 AM
#75
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
Dim myValue as integer = DoSomeCalculation(1, 2)
where to place this code sir?
Please trust me sir........my head is totally locked now...........Now i just want to run the program anyhow......thats it....or i will be dead......Its kind enough enough of you that you are guiding me so much......wasting your time for me sir............
Please tell where to add this line:
Code:
Dim myValue as integer = DoSomeCalculation(1, 2)
-
Jul 19th, 2009, 11:15 AM
#76
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.RenderListAsHTML(DataGridView1)
Dim myValue As Integer = RenderListAsHTML()
End Sub
are you asking me to do this?
-
Jul 19th, 2009, 11:19 AM
#77
Re: Printing problem........Need some help
No...
RenderListAsHTML returns a string - the filename that it has created.
How would you normally get a string back from a function?
-
Jul 19th, 2009, 11:22 AM
#78
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
Dim myValue As String = RenderListAsHTML()
Should i do this sir..........
??????
-
Jul 19th, 2009, 11:25 AM
#79
Re: Printing problem........Need some help
Well half there... you still need to pass in the datagridview.
Dim myValue as string = RenderListAsHTML(DataGridView1)
That will give you the filename of the HTML file that has been created.
Now... do you want to display the HTML in a browser or just print it out?
-
Jul 19th, 2009, 11:29 AM
#80
Thread Starter
Frenzied Member
Re: Printing problem........Need some help
Code:
Imports System.Data
Imports System.Data.OleDb
Imports system.io
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste1\waste1\db1.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
Dim ds As DataSet = New DataSet
DataGridView1.Columns(0).DataPropertyName = "Name"
DataGridView1.Columns(1).DataPropertyName = "Address"
DataGridView1.Columns(2).DataPropertyName = "Roll"
da.Fill(ds, "Table1")
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Function RenderListAsHTML(ByRef DataGrid As DataGridView) As String
Dim swrOutput As StreamWriter
' Dim sFormat As String
Dim sPath As String
'establish a temporary filename
sPath = Path.GetTempFileName
swrOutput = New StreamWriter(sPath)
swrOutput.WriteLine("<head>")
swrOutput.WriteLine("<title>Your Title Here</title>")
swrOutput.WriteLine("</head>")
swrOutput.WriteLine("<style type='text/css'>")
swrOutput.WriteLine("th")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("tr")
swrOutput.WriteLine("{")
swrOutput.WriteLine("font: 8pt verdana")
swrOutput.WriteLine("}")
swrOutput.WriteLine("</style>")
swrOutput.WriteLine("<body>")
swrOutput.WriteLine("<table>")
'column headers
For Each dvgHeader As DataGridViewColumn In DataGrid.Columns
swrOutput.WriteLine("<th bgcolor='silver'>" & dvgHeader.HeaderText & "</th>")
Next
'actual data
For Each dvgRow As DataGridViewRow In DataGrid.Rows
If dvgRow.Visible And Not dvgRow.IsNewRow Then
swrOutput.Write("<tr>")
For Each objCell As DataGridViewCell In dvgRow.Cells
swrOutput.WriteLine("<td>" & objCell.Value.ToString & "</td>")
Next
swrOutput.Write("</tr>")
End If
Next
swrOutput.WriteLine("</table>")
swrOutput.WriteLine("</body>")
swrOutput.Flush()
swrOutput.Close()
Return sPath
End Function
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.RenderListAsHTML(DataGridView1)
Dim myValue As String = RenderListAsHTML(DataGridView1)
End Sub
End Class
This is my entire code sir.........pls check once is it correct or not.......
I would like to print it first(Tats my task for tomorrow)......
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
|