views:

578

answers:

2

I'm trying the find the best way to send a GridView or DataTable in an email.

Page Behind Code:

protected void Page_Load(object sender, EventArgs e)
{
DataTable s1 = Sql.specificReportData(Convert.ToInt32(Session["userID"]));
this.gv.DataSource = s1.DefaultView;
this.gv.DataBind();
}

This generates and binds the data successfully, but if I try and add the contents of gv to a HTML encoded email then the gv part of the email is blank. Do I need to alter the GridView so it's HTML compliant? I can't find an example of how to do this. Any help appreciated.

edit: Gave answer to Solairaya as he gave fuller example, as well as object flushing and disposal. Marked both answers up as they both helped

+2  A: 

Hai alex try this,

Try this (C#):

using System.IO; using System.Text; using System.Net.Mail;

private string GridViewToHtml(GridView gv)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gv.RenderControl(hw);
    return sb.ToString();
}

protected void SendMailButton_Click(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage();
    mail.Body = GridViewToHtml(GridView1);
    mail.IsBodyHtml = true;
    ......
}
public override void VerifyRenderingInServerForm(Control control)
{

}
Pandiya Chendur
Thanks Pandiya. Just tried this but unfortunately the email is empty. I debugged and StringBuilder sb is empty when it gets passed back. The Gridview I'm passing in is definitely being rendered and not null
Alex
Alex give try at this one http://fredrik.nsquared2.com/viewpost.aspx?PostID=412
Pandiya Chendur
+2  A: 

Page behind code

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = getHTML(GridView1);
    }

    private string getHTML(GridView gv) 
    { 
        StringBuilder sb = new StringBuilder(); 
        StringWriter textwriter = new StringWriter(sb); 
        HtmlTextWriter htmlwriter = new HtmlTextWriter(textwriter); 
        gv.RenderControl(htmlwriter); 
        htmlwriter.Flush(); 
        textwriter.Flush(); 
        htmlwriter.Dispose(); 
        textwriter.Dispose(); 
        return sb.ToString(); 
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

Page code

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
            SelectCommand="SELECT [UserID], [Name], [Email] FROM [WEB_Users] WHERE ([Name] LIKE '%' + @Name + '%')">
            <SelectParameters>
                <asp:Parameter DefaultValue="%Moha%" Name="Name" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
solairaja
is the databinding important for this? As I'm already databinding on pageload. When I run without the datasource and databind lines of code sb is null, even when passing a gridview to it that I know has data in it
Alex
if your sending the Gridview along with the data binded. No need to bind the data once again. Just render it and return it.
solairaja
check the gv has the structure and data in it, in debugging mode first. when ur calling the GetHTML Method.
solairaja
hope i have given clear. and it works fine for me. :)
solairaja
Thanks solairaja. Got it to work in the end, the GridView I was using was having issues due to the timing at which it was being DataBound.
Alex