How to send DataGridView contents by email C#

In this article, I will show you how to send GridView content as an email. Let’s suppose you are working on the asp.net web application and you are showing a list of users as a table and your boss asked you to give me all the users in tabular form.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewEmail.aspx.cs"
Inherits="GridViewEmail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
   <asp:GridView ID="GridView1" runat="server"></asp:GridView>
       <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Send Email" /></div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.IO;

public partial class GridViewEmail : MyPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyPage tmpPage = new MyPage();

        HtmlForm form = new HtmlForm();
        form.Controls.Add(GridView1);
        tmpPage.Controls.Add(form);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
        form.Controls[0].RenderControl(htmlWriter);
        string htmlContent = sw.ToString();
        // MailMessage mailMessage = new MailMessage();
        MailMessage mailMessage = new MailMessage("from@from.com", "to@to.com");

        mailMessage.Subject = "My Exported GridView";
        mailMessage.Body = htmlContent;
        mailMessage.IsBodyHtml = true;
        SmtpClient smtpClient = new SmtpClient("your smtp server address");

        smtpClient.Send(mailMessage);


    }
    public DataSet GetData()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("Product");
        DataRow dr;
        dt.Columns.Add(new DataColumn("Price", typeof(Int32)));
        dt.Columns.Add(new DataColumn("DisCount", typeof(Int32)));
        dt.Columns.Add(new DataColumn("SellPrice", typeof(Int32)));
        for (int i = 1; i <= 10; i++)
        {
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i * 2;
            dr[2] = 1 * 3;
            dt.Rows.Add(dr);
        }
        ds.Tables.Add(dt);
        Session["dt"] = dt;
        return ds;
    }
}
MyPage.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for MyPage
/// </summary>
public class MyPage : Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
        GridView grid = control as GridView;
        if (grid != null && grid.ID == "GridView1")
            return;
        else
            base.VerifyRenderingInServerForm(control);

    }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post