Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

email the contents of a datagrid

Status
Not open for further replies.

russellmunday

Technical User
Jul 24, 2003
87
GB
i have been stuffed in at the deep end here only two weeks of asp.
the problem i have is that i need to email the contents of a datagrid preferably in the body of a message can anyone help
 
You'll need to be a little more specific about which columns you need to email and how it needs to be formatted in the email. A simple solution though is to loop through the items in the grid, build your email body string using a StringBuilder and mail it.
Code:
//pseudo-code
StringBuilder sb = new StringBuilder();
foreach(DataGridItem dgItem in myGrid.Items)
{
   sb.Append("Some stuff: " + dgItem.Cells[0].text;
   // I used the first cell as an example
}
MailMessage mm = new MailMessage();
mm.To = "somebody@somewhere.com";
mm.From = "you@somewhere.com";
mm.Subject = "The DataGrid " + DateTime.Now.ToShortDateString();
mm.BodyFormat = MailFormat.Text;
mm.Body = sb.ToString();
mm.Priority = MailPriority.High;
SmtpMail.SmtpServer="127.0.0.1"; //your SMTP Server address
SmtpMail.Send(mm);
 
I have a form that stores its information in a table and retrieves it later on in the code for a final check. I have used a datagrid to show the returned data each row is a separate order, what I need to be able to do is complete an excel order form from each row in the datagrid.
And email the excel orders.
I would also like to use the datagrid in the body of an email as a conformation to the customer.
There is an added problem that I do not know how many rows there will be.
I have written the code to email with attachments but I am not sure how to get the data from the datagrid one row at a time.
 
I would also like to use the datagrid in the body of an email as a conformation to the customer.
There is an added problem that I do not know how many rows there will be.
I have written the code to email with attachments but I am not sure how to get the data from the datagrid one row at a time.
That's exactly what Veep's code does above. e.g.
Code:
foreach(DataGridItem dgItem in myGrid.Items)
{
   sb.Append("Some stuff: " + dgItem.Cells[0].text;
   // I used the first cell as an example
}
He loops through each row of the datagrid (therefore it doesn't matter how many rows there are), adds them to a string which later becomes the body of the email, and then emails the mail to the email address.


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top