First step: convert gridview to html table and remove the columns you don't want
private string getHTML(GridView gv)
{
this.OrderGridView.AllowPaging = false;
this.OrderGridView.AllowSorting = false;
this.OrderGridView.EditIndex = -1;
// Let's bind data to GridView
this.RefreshGrid();
OrderGridView.Columns.RemoveAt(1);
OrderGridView.Columns.RemoveAt(0);
OrderGridView.DataBind();
// Let's output HTML of GridView
StringBuilder sb = new StringBuilder();
StringWriter textwriter = new StringWriter(sb);
HtmlTextWriter htmlwriter = new HtmlTextWriter(textwriter);
OrderGridView.RenderControl(htmlwriter);
htmlwriter.Flush();
textwriter.Flush();
htmlwriter.Dispose();
textwriter.Dispose();
return sb.ToString();
}
Then send it via email:
protected void Button1_Click(object sender, ImageClickEventArgs e)
{
try
{
string emailGR = getHTML(OrderGridView);
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient smptClient = new System.Net.Mail.SmtpClient("mail.*****.***");
smptClient.Credentials = new System.Net.NetworkCredential("**@****.gr", "*****)d");
mail.Body = "New Order From: " + " " + username.Text + "<br/>" + "Total Amout:" + " " + sum.Text + "<br/>" + "Notes:" + " " + TextBox5.Text + emailGR;
mail.IsBodyHtml = true;
mail.To.Add(new System.Net.Mail.MailAddress("****@****.**"));
mail.From = new MailAddress("customer@****.***");
mail.Subject = "New Order From" + " " + username.Text;
smptClient.Send(mail);
string Msg = "<script>alert('You have successfully sent your order!!');</script>";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
//Response.Write(@"<script language='javascript'>alert('You have successfully sent your order!')</script>");
}
catch
{
string Msg = "<script>alert('Mail Sending Failed..try again!');</script>";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
//Response.Write(@"<script language='javascript'>alert('Mail Sending Failed..try again!')</script>");
}
this.OrderGridView.AllowPaging = true;
this.OrderGridView.AllowSorting = true;
this.OrderGridView.EditIndex = -1;
this.RefreshGrid();
code.Focus();
}