Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a GridView in my web application. Having 9 Columns. I just need to send the detail of 6 columns from it through email. What will be the accurate procedure of this issue. Also how I will convert those column into HTML format.... Please Help.
Posted
Updated 12-May-14 1:14am
v2
Comments
CHill60 12-May-14 7:15am    
Rather a lot of your old questions are reappearing in, actually flooding, the list in QA - any particular reason why you are editing solved questions?

You can take you gridview into datatable and then you can remove n number of columns using dt.column.remove(columnname) something like that and then you can convert your datatable to htmltable by using function given in below link and then send this html table in you email..

http://samuellam.wordpress.com/2010/11/18/render-datatable-as-html-table/[^]
 
Share this answer
 
Comments
AR547 18-Sep-13 4:33am    
Can u explain this from above link...

foreach (DataColumn c in dt.Columns)
{
sb.AppendFormat("<th>{0}</th>", c.ColumnName);
}
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:
XML
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();

       }
 
Share this answer
 
v3
Hi...
See this. May its useful to u.
C#
//Reads values from ur gridview1.
GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
//assigning gridview1 controls to strings like blow.
string username = ((Label)GridView1.Rows[gvRow.RowIndex].FindControl("lblusername")).Text;
string fathername = ((Label)GridView1.Rows[gvRow.RowIndex].FindControl("lblfathername")).Text;
string address= ((Label)GridView1.Rows[gvRow.RowIndex].FindControl("lbladdress")).Text;
string zipcode= ((Label)GridView1.Rows[gvRow.RowIndex].FindControl("lblzipcode")).Text;
string mailid= ((Label)GridView1.Rows[gvRow.RowIndex].FindControl("lblmailid")).Text;
string phoneno= ((Label)GridView1.Rows[gvRow.RowIndex].FindControl("lblphoneno")).Text;

like that reads the values from ur GridViewRow1.After that sends that values through mail.
Thank u.
 
Share this answer
 
Comments
AR547 20-Sep-13 0:09am    
This is all for item templates. I have bound fields in my gridview and 2 item templates only. But its helpful tip. Thanks :)
THERE ARE TWO TABLES WHICH I AM SENDING TO EMAIL.
• HTML TABLE - GETTING VALUES OF MY SERVER CONTROLS IN THE HTML TABLE
• GRIDVIEW - AFTER CONVERTING IT TO HTML.
SENDING THE HTML TABLE AND GRIDVIEW(HTML FORMAT) TO EMAIL

C#
private void emaildatatable()
    {
        string reqini = ExEMP_PERSONAL.getEmpName(Session["empCode"].ToString());

        StreamReader reader = new StreamReader(Server.MapPath("~/Email Templates/old/RequestHeader.htm"));
        string readFile = reader.ReadToEnd();
        string substring = "";
        substring = readFile;
        substring = substring.Replace("#ParentRequestNo#", parentTaskNo);
        substring = substring.Replace("#RequestInitiator#", reqini);
        substring = substring.Replace("#RequestDate#", Label39.Text);
        substring = substring.Replace("#ApprovingAuthority#", DropDownList3.SelectedItem.Text);
        substring = substring.Replace("#ApprovalHeader#", "Approval");
        substring = substring.Replace("#ApprovalDisapprovalDate#", "N/A");
        substring = substring.Replace("#RequestNotifications#", "");

        string body = "<h1 align="center">Change Management System</h1><br>";
        body += "<br><br>";
        string to = ExEMP_PERSONAL.getEmpEmail(Session["empCode"].ToString());
        MyEmail mm = new MyEmail();
        body += substring.ToString();
        body += "<br><br>";
        body += "<h3 align="center">Account Closing Request</h3>";
        body += GridViewToHtml(GridView3).ToString();
        mm.sendEmail(to, "", "", "Your Request No. " + parentTaskNo + " is Submitted for Approval -- ( " + tskctrl.getTaskImpact(parentTaskNo) + " )", body);
        
    }

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();
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900