Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've been trying to create a report on an email body. But now I have to insert the data related to one person. For example:

I have a guy named Carl that has a subscription. I'm able to load all the data to the textboxes, but now I need to know how to create a disposable data table and use it to store textbox values accordingly to the client in question. After that, I want to dynamically create a table that some columns related to that data table and paste it into the email body.

Here is the sample code regarding table creation using datagridview:

What I have tried:

C#
foreach (DataGridViewColumn col in dgw.Columns)
                {
                    mailBody += "<td>" + col.HeaderText + "</td>";
                }
                mailBody += "</tr>";
                foreach (DataGridViewRow row in dgw.Rows)
                {
                    mailBody += "<tr>";
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        mailBody += "<td>" + cell.Value + "</td>";
                    }
                    {
                        mailBody += "</tr>";
                    }
                }
Posted
Updated 5-Jun-17 2:18am
v2
Comments
ZurdoDev 5-Jun-17 7:45am    
What's wrong with the code you have?
Scribling Doodle 5-Jun-17 7:56am    
This code I used to use for Datagrid, my problem is that I cannot figure out a way to make it work for Datatables instead.
ZurdoDev 5-Jun-17 7:58am    
Instead of looping through the grid, loop through the rows in a DataTable. The logic is the same.
Scribling Doodle 5-Jun-17 8:14am    
I've found a better solution to keep the integrity of the table on the email not so unusual. This one creates a more flat grid and works for datatables too
ZurdoDev 5-Jun-17 8:18am    
OK. Please post something as a solution so that this no longer shows up in the Unanswered list of questions.

1 solution

I found a better and cleaner way to display the values from a disposable datatable called dt. This code should work as intended, create a table on the email body, by using the disposable table that's created to store the values.

C#
DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ID"));
                

                DataRow dr = dt.NewRow();
                dr[0] = "ID -" + txtSomething.Text;
                

                dt.Rows.Add(dr);
                MailMessage mm = new MailMessage("your_email@something.com", "your_email@something.com");
                StringBuilder sb = new StringBuilder();
                sb.Append("<p><table width='100%' style='border:Solid 1px Black;'>"); for (int colIndx = 0; colIndx < dt.Columns.Count; colIndx++)
                {

                    sb.Append("<th>");
                    sb.Append(dt.Columns[colIndx].ColumnName);

                    sb.Append("</th>");
                }

                sb.Append("</thead>");

                for (int rowIndx = 0; rowIndx < dt.Rows.Count; rowIndx++)
                {

                    sb.Append("<tr>"); for (int colIndx = 0; colIndx < dt.Columns.Count; colIndx++)
                    {

                        sb.Append("<td>");
                        sb.Append

                        (dt.Rows[rowIndx][colIndx].ToString());

                        sb.Append("</td>");
                    }

                    sb.Append("</tr>");
                }

                sb.Append("</table>");
                mm.Body = sb.ToString();

                mm.IsBodyHtml = true;
 
Share this answer
 

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