Home > Net >  How to put two datagridview ruled in the email body?
How to put two datagridview ruled in the email body?

Time:11-04

Problem description:
Winform interface with multiple datagridview, respectively corresponding to the data, need to put them in the E-mail sent to the department colleagues, now I can achieve this function, the practice of using HTML, but found the datagridview only on one by one in the mail, if the form is many, the mail will be very long, and inconvenient comparative view, therefore, seek can take two forms and methods, at least can make email height in half, but seems no matter how to change the HTML can achieve this purpose,
For everyone a great god help me have a look at my code how to modify?
In addition, if there's a function code? Interface screenshots down, put the pictures in the email body to send out?


Private void button5_Click (object sender, EventArgs e)//automatic email
{
DataTable dt1=(DataTable) enclosing dataGridView2. The DataSource.
DataTable dt2=(DataTable) enclosing dataGridView4. The DataSource.

Outlook. Application olApp=new Outlook. Application ();
Outlook. MailItem MailItem=(Outlook. MailItem) olApp. The CreateItem (Outlook. OlItemType. OlMailItem);
MailItem. To="XXXXXX";
MailItem. Subject="mailtitle";
MailItem. BodyFormat=Outlook. OlBodyFormat. OlFormatHTML;
MailItem. HTMLBody="mailcontent";
MailItem. HTMLBody=ExportDatatableToHtml (dt1 as DataTable) + ExportDatatableToHtml (dt2 as DataTable);

(Outlook. _MailItem mailItem). The Display ();
MailItem=null;
OlApp=null;
MessageBox. Show (" email success ");
}

Public static string ExportDatatableToHtml DataTable (dt)
{
StringBuilder sb=new StringBuilder();
Sb. Append (" & lt; HTML & gt;" );
Sb. Append (" & lt; Head>" );
Sb. Append (" & lt;/head>" );
Sb. Append (" & lt; Body>" );
Sb. Append (" & lt; The table style="width: auto; border-collapse:collapse; Border: 1 px solid # 000000; Text text-indent: 1 em; Padding - right: 1 em '& gt;" );

Sb. Append (" & lt; Tr & gt;" );
Foreach (DataColumn dc in dt. The Columns)
{
Sb. Append (" & lt; Td style='border: 1 px solid # 000000 & gt;" );
Sb. Append (dc. ColumnName);
Sb. Append (" & lt;/td>" );
}
Sb. Append (" & lt;/tr>" );

Foreach (DataRow Dr In dt. Rows)
{
Sb. Append (" & lt; Tr & gt;" );
Foreach (DataColumn myColumn in dt. The Columns)
{
Sb. Append (" & lt; Td style='border: 1 px solid # 000000; text-align:center; '& gt;" );
Sb. Append (Dr [myColumn ColumnName]. ToString ());
Sb. Append (" & lt;/td>" );
}
Sb. Append (" & lt;/tr>" );
}
Sb. Append (" & lt;/table>" );
Sb. Append (" & lt;/body>" );
Sb. Append (" & lt;/html>" );
Return sb. ToString ();
}

Winform system interface:


The email body pattern:


Hope the email body pattern:

CodePudding user response:

 & lt; Table> 

Table1 & lt;/td>
Table2 & lt;/td>


Table3 & lt;/td>
Table4 & lt;/td>

CodePudding user response:

reference 1/X - I - n reply:
 & lt; Table> 

Table1 & lt;/td>
Table2 & lt;/td>


Table3 & lt;/td>
Table4 & lt;/td>


Thank you,
I have thought of that way, but don't know this code on ExportDatatableToHtml as variables, or written in button5,
In addition, the way will have two tables together, I hope is to separate, or you don't look convenient, moreover this way requires the number of rows in the table is the same? I this a few datagridview may rows are different, this way can succeed?
Please advice,

CodePudding user response:

And the last post, "write programs according to the logic, not according to the technique"

Your logic is into HTML "Mr", so it is not looking for us, but to look for the design of your company, let him give you a mock data and in the way of typesetting typesetting HTML that you want to you

Finish this step, you are to consider how to use the realization of the code to generate the HTML

And techniques, code generation, text, we can use the text template engine, such as Razor, T4, or any other text template engine (datagridview with what you say it doesn't matter, in the field of text template engine, we only care about, the data and the template, and don't care about datagridview)

CodePudding user response:

snails to learn programming reference 2 floor response:
Quote: refer to 1st floor X - I - n reply:
 & lt; Table> 

Table1 & lt;/td>
Table2 & lt;/td>


Table3 & lt;/td>
Table4 & lt;/td>


Thank you,
I have thought of that way, but don't know this code on ExportDatatableToHtml as variables, or written in button5,
In addition, the way will have two tables together, I hope is to separate, or you don't look convenient, moreover this way requires the number of rows in the table is the same? I this a few datagridview may rows are different, this way can succeed?
Ask for advice,

According to your writing, ExportDatatableToHtml is better,
Is not the same as the number of lines as a result of the different height, height is not the same as a result, the same lines of two forms, the form of 100% high, short of the form of less than 100% is high, the bottom will be automatically left blank, then the style can meet the requirements? If you don't meet, you hope is just change what; If we can meet, there is no problem, so ultimately depends on your own needs,

CodePudding user response:

There is something wrong with the above statement, according to your original intention, ExportDatatableToHtml datatable exported as a table, don't have to move on, this part of the code in button5, put through ExportDatatableToHtml several more appropriate table, they

CodePudding user response:

The
reference 5 floor X - I - n reply:
there is something wrong with the above statement, according to your original intention, ExportDatatableToHtml datatable exported as a table, don't have to move on, this part of the code in button5, put through ExportDatatableToHtml several more appropriate table, they


Your meaning is change the code?
The assembled statement how to write? Can be arranged to help me modify the not?
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related