1

I want to send a GridView by email from my Exchange (outlook) account using an asp:button.

My code works fine, but the GridView loose it format (colors, column weight, etc) in the email.

This is the code:

using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;

protected void Send_Click(object sender, EventArgs e)
        {
            try
            {
                // Create a Outlook Application and connect to outlook 
                Outlook.Application OutlookApplication = new Outlook.Application();

                // create the MailItem which we want to send 
                Outlook.MailItem message = (Outlook.MailItem)OutlookApplication.CreateItem(Outlook.OlItemType.olMailItem);

                MailAddress toAddress = new MailAddress("myemail@gmail.com");
                MailAddress ccAddress = new MailAddress("myemail@mycompany.com");

                message.To = toAddress.ToString();
                message.CC = ccAddress.ToString();
                message.Subject = "Mail Subject";
                message.HTMLBody = GridViewToHtml(HourlyReportGV1);
                message.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

                //Send email
                message.Send();
            }
            catch (System.Exception ex)
            {
                Aviso.Text = ex.ToString();
            }
        }

        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();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }

This is how it looks:

enter image description here

Do you know how to made the "Email View" exactly the same as the "Web View"?

<asp:GridView style="margin:0 auto; margin-bottom:50px;" HeaderStyle-CssClass="tituloshandoff" RowStyle-CssClass="contenidohandoffbatch" ID="HourlyReportGV1" runat="server" AutoGenerateColumns="False" DataSourceID="Vacio">
            <Columns>
                <asp:BoundField DataField="IDBatch" HeaderText="ID" SortExpression="IDBatch" HeaderStyle-CssClass="TituloInvisible" ItemStyle-CssClass="TituloInvisible"/>
                <asp:BoundField DataField="DealerCodigo" HeaderText="CODE" SortExpression="DealerCodigo"/>
                <asp:BoundField DataField="DealerDescripcion" HeaderText="NAME" SortExpression="DealerDescripcion"/>
                <asp:TemplateField HeaderText="00:00"><ItemTemplate><asp:TextBox runat="server" ID="Cero" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="01:00"><ItemTemplate><asp:TextBox runat="server" ID="Una" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="02:00"><ItemTemplate><asp:TextBox runat="server" ID="Dos" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="03:00"><ItemTemplate><asp:TextBox runat="server" ID="Tres" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="04:00"><ItemTemplate><asp:TextBox runat="server" ID="Cuatro" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="05:00"><ItemTemplate><asp:TextBox runat="server" ID="Cinco" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="06:00"><ItemTemplate><asp:TextBox runat="server" ID="Seis" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="07:00"><ItemTemplate><asp:TextBox runat="server" ID="Siete" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="08:00"><ItemTemplate><asp:TextBox runat="server" ID="Ocho" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="09:00"><ItemTemplate><asp:TextBox runat="server" ID="Nueve" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="10:00"><ItemTemplate><asp:TextBox runat="server" ID="Diez" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="11:00"><ItemTemplate><asp:TextBox runat="server" ID="Once" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
            </Columns>
        </asp:GridView>
  • Can you show us the markup of the GridView? – ConnorsFan May 22 '16 at 12:25
  • do you mean the code? Sorry, Im from Argentina, I don't know what you are talking about with "markup". EDIT: I'm reading about "GridView Skins" right now. I didn't know about it. –  May 22 '16 at 15:04
  • Your update is what I was talking about (the HTML or pseudo-HTML code in the ASPX file). As for your problem, the CSS styles (`tituloshandoff`, `contenidohandoffbatch`) are not sent in the e-mail, so their formatting options cannot be applied in the message. You may try to set individual properties in the markup to see if the message can honor them (e.g. `HeaderStyle-BackColor="Orange" RowStyle-Height="24"`). – ConnorsFan May 22 '16 at 15:18
  • Understood. Let me try, I will let you know. Thanks! –  May 22 '16 at 15:35
  • I try with your reccomendation, and it's working ok. I need only one more help here: - The gridview is showing information from a database. To work with it, I need to extract the "ID" for each item in GridView. That's why I use the "ID" column. But I need it to be invisible, in the webform and in the email. I can do it invisible in the webform, but using css. The email doesn't take the css information, then this column is showed. How can I solve it? –  May 22 '16 at 19:50
  • If you need the `ID` value only in code-behind, I suggest to store it in the `DataKeyNames` property of the GridView (http://stackoverflow.com/questions/36827111/asp-net-gridview-how-to-edit-and-delete-data-records/36828018#36828018, http://stackoverflow.com/questions/36813634/how-to-retrieve-the-value-of-a-listview-item-in-the-same-row-as-a-linkbutton/36814031#36814031). – ConnorsFan May 22 '16 at 20:49
  • Thank you! could you please send it as answer? So I can vote it. –  May 26 '16 at 11:49
  • Thanks Denis! I added the answer, which addresses the specific problem mentioned in your question. As for retrieving the ID value, I don't give the method here since it is detailed in the other posts mentioned above (but you can upvote one of them if it helped you). – ConnorsFan May 26 '16 at 15:07

1 Answers1

0

The CSS style classes that are used by your GridView (HeaderStyle-CssClass="tituloshandoff", RowStyle-CssClass="contenidohandoffbatch") are not included in the HTML code generated by gv.RenderControl(hw). As a consequence, their style attributes cannot be applied to the GridView table in the e-mail message.

You can set individual style properties in the GridView markup (e.g. HeaderStyle-BackColor="Silver", RowStyle-Height="24"). These style attributes will then be embedded in the GridView HTML output, and the e-mail application will have the opportunity to honor them.

ConnorsFan
  • 70,558
  • 13
  • 122
  • 146