Export Datagrid to HTML
If you want to export the contents of Datagrid and save it as file or send it by email… there is a simpler way ..
Private Sub btnDataGridEmail_Click(object sender, System.EventArgs e)
SendEmail(getHTML(datagrid1))
End Sub——-
Private Function getHTML(ByVal dataGrid1)Dim result As String
Dim stringwriter As New StringWriterDim htmlwriter As HtmlTextWriterhtmlwriter = New HtmlTextWriter(stringWriter)Try
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html)htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body)
DataGrid1.RenderControl(htmlWriter)
htmlWriter.RenderEndTag()
htmlWriter.RenderEndTag()
htmlWriter.Flush()
result = stringWriter.ToString()
Catch ex As Exception‘Error handlersFinally
htmlwriter.Close()stringWriter.Close()
getHTML = result
End Try
End Function——————-
Public Function SendEmail(ByVal sMessage As String) As Integer
Dim sFromEmail As StringDim sToEmail As String
Dim sSubject As StringDim sSmtp As String
sFromEmail = ConfigurationSettings.AppSettings(“MailFrom”)
sToEmail = ConfigurationSettings.AppSettings(“MailTo”)
sSubject = “DataGrid Values”
sSmtp = ConfigurationSettings.AppSettings(“MailServer”)Try
Dim TheMailMessage As New MailMessageDim TheMailConnection As SmtpMailTheMailMessage.From = sFromEmailTheMailMessage.To = sToEmail‘Request.ServerVariables(“REMOTE_ADDR”) : returns the IP
‘ System.Environment.MachineName : returns the Machine Name (when multiple servers are in load balancing)
TheMailMessage.Subject = sSubject & ” – ” & Request.ServerVariables(“REMOTE_ADDR”) & ” – ” & System.Environment.MachineName
TheMailMessage.Body = sMessage
TheMailMessage.BodyFormat = MailFormat.Html
TheMailConnection.SmtpServer = sSmtp
TheMailConnection.Send(TheMailMessage)
TheMailConnection = Nothing
TheMailMessage = NothingReturn Nothing
Catch ex As Exception
Return Nothing
End TryEnd Function
Leave a Reply