email a gridview
February 5, 2009 by: B.HardingIn this post I’ll discuss how I coerced a gridview control into straight text. The reason I did this was to send the data from the gridview in an email. An invoice in this case.
At first I figured I could use a div and simply tap in to the inner html. No luck! It’s not available on the server side. I’d like to avoid the help of Javascript so I kept looking.
I decided to make my own copy of text that is generated when the grid is databound. Every time a row is databound I append the info to my string. This turned out to be pretty flexible. I can pretty much turn the data into anything I want. csv, or a html table. At first I tried plain text with tabs in between. I decided to use an html table as it ended up looking the best.
In the grid’s row databound event I add <table> if the row is a header row. I close it with </table> if the row is the footer. For everyting in between I create a <tr> then for each control with <td>
I saved this text in a hidden label that I can call later to be used in the body of an email.
Here is the source code. I use linq to poulate the gridview but you can bind the gridview to any datasource.
<%@ Page Language=”VB” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
‘make some data with Linq
Dim myList() = {New With {.Name = “Bill”, .Title = “Developer”, .Age = “21″, .Birthday = “9/30″}, _
New With {.Name = “Joe”, .Title = “Tester”, .Age = “23″, .Birthday = “1/3″}, _
New With {.Name = “Tom”, .Title = “Senior Developer”, .Age = “25″, .Birthday = “3/20″}, _
New With {.Name = “Mark”, .Title = “Technician”, .Age = “41″, .Birthday = “6/26″} _
}
Me.GridView1.DataSource = myList
Me.GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
Label1.Text &= vbCrLf & “<table border=’0′cellpadding=’5′ cellspacing=’5′>” & vbCrLf
End If
If e.Row.RowType <> DataControlRowType.Footer Then
Label1.Text &= vbTab & “<tr>” & vbCrLf
For Each item In e.Row.Controls
If item.visible = True Then
Label1.Text &= vbTab & vbTab & “<td>”
Label1.Text &= item.text
Label1.Text &= “</td>” & vbCrLf
End If
Next
Label1.Text &= vbTab & “</tr>” & vbCrLf
End If
If e.Row.RowType = DataControlRowType.Footer Then
Label1.Text &= “</table>” & vbCrLf
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label2.Text = Server.HtmlEncode(Label1.Text)
Me.Label1.Visible = True
‘ Dim msg As New System.Net.Mail.MailMessage(TO@EMAIL.COM, FROM@EMAIL.COM)
‘ msg.Subject = “Gridview email”
‘ msg.IsBodyHtml = True
‘ msg.Body = Me.Label1.Text
‘Dim smtp As New System.Net.Mail.SmtpClient
‘ smtp.Send(msg)
End Sub
</script>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div id=”div1″ runat=”server”>
<asp:GridView ID=”GridView1″ runat=”server” onrowdatabound=”GridView1_RowDataBound” AutoGenerateColumns=”False” >
<Columns>
<asp:BoundField DataField=”name” HeaderText=”Name” />
<asp:BoundField DataField=”age” HeaderText=”Age” />
<asp:BoundField DataField=”title” HeaderText=”Title” />
<asp:BoundField DataField=”birthday” HeaderText=”Birthday” />
</Columns>
</asp:GridView>
<asp:Button ID=”Button1″ runat=”server” Text=”capture this ^” onclick=”Button1_Click” />
<br />
<asp:Label ID=”Label1″ runat=”server” Visible=”False”></asp:Label>
<pre>
<asp:Label ID=”Label2″ runat=”server” Visible=”true”></asp:Label>
</pre>
</div>
</form>
</body>
</html>


