-
Re: Printing problem........Need some help
That looks OK at first glance, but obviously I can't test it.
In order to print it I would create a webbrowser control, navigate to the file (you have the filename now) and then use the webbrowser.print method. The catch is waiting until until the document has loaded into the webbrowser.
-
Re: Printing problem........Need some help
Please help me sir........
-
Re: Printing problem........Need some help
ok sir then how to do this........Please help me......
-
Re: Printing problem........Need some help
Actually no its still wrong.
Code:
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
That first line is pointless, your 2nd line calls the function. If you leave that first line in then it will create two HTML files representing your grid, the first one you then ignore.
-
Re: Printing problem........Need some help
I'm taking a break for an hour or so - see how far you get with creating a webbrowser and getting it to open up your HTML and I'll check back in a bit.
-
Re: Printing problem........Need some help
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myValue As String = RenderListAsHTML(DataGridView1)
End Sub
I think its right now sir...
-
Re: Printing problem........Need some help
Still I need your help sir in creating a browser and printing it......
I am waiting over here sir..........
-
Re: Printing problem........Need some help
OK... how far have you got?
-
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
Dim myValue As String = RenderListAsHTML(DataGridView1)
End Sub
End Class
Here is my code sir......
Now what to do?
-
Re: Printing problem........Need some help
I mean with the webbrowser aspect - thats where you were about an hour ago isn't it?
I hope you weren't just waiting for me to come back - I'm not really into writing every line of code for people, especially if it is an assignment.
-
Re: Printing problem........Need some help
I know that sir.....If i were not into that excel then i might have tried it from yesterday....
But now its the last moment.....I am helpless and very afraid......about this printing.....
I need your help sir.......Please help me sir for today at least.......
-
Re: Printing problem........Need some help
Well the quick and dirty way to do it would be to add a web-browser control to your form and make it invisible.
Then in your button2_click event you would call the webbrowser control's navigate method, passing in the filename of your HTML file.
Then once it is loaded you call its print method. Then you delete the HTML file so you aren't littering your machine's temp folder with old files.
The nicer way to do it would be to create the webbrowser control dynamically but that requires slightly more advanced coding and I'm not sure either of us have the inclination to do that at this point!
-
Re: Printing problem........Need some help
You gave me the entire coding till now sir......
I you can give me the dynamically creating web browser code then also i might do it......I wont sleep today even and I might do it........
Now you say sir which way to go now....
I need your help sir.........
-
Re: Printing problem........Need some help
If it is for an assignment I'd say rather you just do it the quick and dirty way as I'm sure you can do this easily enough yourself, rather than me giving you code that you don't understand.
If you want to try your own code and post it I'll help you out where you are stuck. Either way to tackle it, there are only something like 5 lines of code to write!
-
Re: Printing problem........Need some help
Ok sir ........If there is something 5 lines of code to create it dynamically.....Then i think i should go for the dynamic creation sir.......
First let me try out the code......Then i might understand it today.....
Then only go to bed else no ......
But I need your help sir....Please
-
Re: Printing problem........Need some help
OK so how do you create a control dynamically? Bear in mind that controls are just classes.
-
Re: Printing problem........Need some help
First please give me the concept of creating dynamically sir.....
-
Re: Printing problem........Need some help
You will have done this many times before, but maybe you didn't realise what you were doing.
Say you have defined a class called MyType, and you want to create an object of this type :
Code:
Dim myVariable = New MyType
Controls aren't really any different, so at its simplest you just need
Code:
Dim myWebBrowser = New WebBrowser
will create a new webbrowser control and just hold it in memory (it won't appear on any form). You need to place this declaration in the local variables declaration space (ie between the "Public Class Form1" line and your first button click event.
This declaration will need some modification which I will explain shortly once you've set up your control to load up the HTML file.
Once that has been declared you can access it from within the Button2_click event - after you've made the call to create the HTML file.
-
Re: Printing problem........Need some help
Ok sir ....
I added this code in between the "Public Class Form1" line and first "button click" event
Code:
Public Class Form1
Dim myWebBrowser = New WebBrowser
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Now what should i do?
-
Re: Printing problem........Need some help
Now sir how to set up control to load up the HTML file?
-
Re: Printing problem........Need some help
If you scroll back up to post #92 I told you there how to get a webbrowser control to load a page... and in post #98 I told you where to do it.
-
Re: Printing problem........Need some help
Are you talking about this web custom control?
Attachment 72084
I added this sir and a code appears under the "webCustomControl1.vb"
Code:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1
Inherits WebControl
<Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
Get
Dim s As String = CStr(ViewState("Text"))
If s Is Nothing Then
Return String.Empty
Else
Return s
End If
End Get
Set(ByVal Value As String)
ViewState("Text") = Value
End Set
End Property
Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
output.Write(Text)
End Sub
End Class
am i right sir?
Then what should I do sir?
-
Re: Printing problem........Need some help
How to make the web custom control invisible sir?
-
Re: Printing problem........Need some help
Nope - way off the mark I'm afraid. You don't need to add anything new - you've already created the webbrowser control - thats what that last line did.
Now you want to use the webbrowser that you have created - myWebBrowser - to navigate to your HTML file.
There's a big hint in that last sentence as to what you need to do...
-
Re: Printing problem........Need some help
Code:
Dim myWebBrowser = New WebBrowser
In this code i created the control.......
Now i want to use the myWebBrowser.......
am i right sir?
Now what should i do sir.......
-
Re: Printing problem........Need some help
Yes thats where we were back up at post #100 and as I replied there to look at posts #92 and #98
-
Re: Printing problem........Need some help
I am getting your point sir....
But how and where to make the web browser control invisible sir?
Now in button2_click event I would call the webbrowser control's navigate method, passing in the filename of the HTML file
How to do this sir?
I cant get it........Please help me
-
Re: Printing problem........Need some help
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myValue As String = RenderListAsHTML(DataGridView1)
WebBrowser.NavigateEventArgs()
End Sub
Is this what you asked me to do?
-
Re: Printing problem........Need some help
No. For a start WebBrowser isn't the name of the webbrowser control you created, it is the name of the class.
What is the name of the webbrowser you just created?
Quote:
But how and where to make the web browser control invisible sir?
You don't need to make it invisible because it has never been made visible.
-
Re: Printing problem........Need some help
Help me sir.............please
-
Re: Printing problem........Need some help
Please give me the code first......let me run it.......check it .........then i will make my concept clear.....
I am rather getting confuder sir..........
My heart beat is also getting up because its late night here and i have to finish this .........
-
Re: Printing problem........Need some help
Code:
Imports System.Data
Imports System.Data.OleDb
Imports system.io
Public Class Form1
Dim myWebBrowser = New WebBrowser
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
Dim myValue As String = RenderListAsHTML(DataGridView1)
WebBrowser.NavigateEventArgs()
End Sub
End Class
here lies my entire code sir.....
-
Re: Printing problem........Need some help
Right - your new control is called MyWebBrowser. So you use that.
The method for Navigating to a page is called Navigate. And it needs to be told what file or URL to navigate to, so you need to pass it the name of the file we just created.
-
Re: Printing problem........Need some help
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myValue As String = RenderListAsHTML(DataGridView1)
myWebBrowser.NavigateEventHandler()
End Sub
But sir which file to pass in this code?
Code:
myWebBrowser.NavigateEventHandler()
Please help me sir......
-
Re: Printing problem........Need some help
Firstly.. it is "Navigate" not "NavigateEventHandler" that you need.
Then what you need to tell it is where to look for the HTML, and the line above sets the location of the HTML file we created into a variable called MyValue.
So how about "myWebBrowser.Navigate(MyValue)"?
Then what you need to do is work out how you can tell when it has finished loading the HTML into the browser. How do you reckon -in general terms - what kind of mechansim you need for that? Ie how to controls notify you when something has happened to them?
-
Re: Printing problem........Need some help
Code:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myValue As String = RenderListAsHTML(DataGridView1)
myWebBrowser.Navigate(myValue)
End Sub
How should i indicate the finishing of the HTML into the browser sir?
I dont know sir.......
next what to do sir?
-
Re: Printing problem........Need some help
OK the answer to my question is... Events...
What event does the webbrowser control raise that may help us here? For some help here's the relevant page on the MSND
-
Re: Printing problem........Need some help
Is it the navigated event sir?
Am i right?
-
Re: Printing problem........Need some help
-
Re: Printing problem........Need some help
Not quite - that event fires when it gets to the page you want and starts downloading it, you actually want documentcompleted which fires when it has finished downloading the page.
In order to use events with your newly created webbrowser you need to slightly modify your declaration from
Code:
Dim myWebBrowser = New WebBrowser
to this :
Code:
Dim WithEvents MyWebBrowser As New WebBrowser
So if you make that change, then in the left-hand dropdown box at the top of the code pane in your code editor, you should see "MyWebBrowser". Click on that and then click in the right-hand dropdown and pick DocumentComplete and it will create this code for you :
Code:
Private Sub MyWebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles MyWebBrowser.DocumentCompleted
End Sub
Then inside that block you need to get the webbrowser control to print, using one of its built in methods - I'll leave it to you to work out which one.