我对Datagrid有点问题。
从SQL获取的数据网格 - >
要点是:
- 第一列有数字,
- 最后一栏(第8栏),附电子邮件。
我希望通过Outlook(正文中的表格)发送HTML Body,其中包含行数据列表,其中第1列(数字)相同,对于存储在8日的电子邮件。循环中将分组信息发送到每个号码。
现在我每个电子邮件使用单独的行,但我需要从第1列(行列表)中为每个值配置组。在我的样本下面:
con.Open();
String query = "xxxxxxxx";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
SDA.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
for (int i = 0; i < DataGridView.Rows.Count; ++i)
{
1 = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
2 = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
3 = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
4 = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);
5 = Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value).ToString("N");
6 = Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value).ToString("N");
7 = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value).ToString("N");
Outlook.Application app = new Outlook.Application();
Outlook.MailItem mailItem = app.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "xxx" + 1 + "xxx" + 2 + "xxx";
mailItem.To = "xxxx@xxxxx";
mailItem.HTMLBody = "aaaaaaaaa" + 1 + "bb: " + 2 + "vvvv."
mailItem.Importance = Outlook.OlImportance.olImportanceHigh;
mailItem.Display(false);
((Outlook._MailItem)mailItem).Send();
}
我已尝试过:
string mailBody = "<table width='100%' style='border:Solid 1px Black;'>";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
mailBody += "<tr>";
foreach (DataGridViewCell cell in row.Cells)
{
mailBody += "<td style='color:blue;'>" + cell.Value + "</td>";
}
mailBody += "</tr>";
}
mailBody += "</table>";
mailItem.HTMLBody = mailBody;
但是在这种风格中,我将完整的DataGrid整合到一封电子邮件中。
你能帮帮我怎么做吗?
答案 0 :(得分:1)
试试这个......
DataTable dt = dataGridView1.DataSource as DataTable;
//assuming your first columns name is 1...
var groups = dt.AsEnumerable().GroupBy(x => x["1"]);
foreach (var group in groups)
{
// create your mailItem instance....
string mailBody = "<table width='100%' style='border:Solid 1px Black;'>";
List<string> mailsToSend = new List<string>();
foreach (var row in group)
{
mailBody += "<tr>";
mailItem.Recipients.add(row.ItemArray[7].toString());
foreach (var cell in row.ItemArray)
{
mailBody += "<td style='color:blue;'>" + cell.ToString() + "</td>";
}
mailBody += "</tr>";
}
mailBody += "</table>";
mailItem.HTMLBody = mailBody;
//Send your mail....
}
<强>更新强>
您可以创建一个包含唯一邮件的邮件列表...
foreach (var group in groups)
{
// create your mailItem instance....
List<string> mailsToSend = new List<string>();
string mailBody = "<table width='100%' style='border:Solid 1px Black;'>";
foreach (var row in group)
{
mailBody += "<tr>";
// if not in the list, simply add...
if (!mailsToSend.Contains(row.ItemArray[7].ToString()))
{
mailsToSend.Add(row.ItemArray[7].ToString());
}
foreach (var cell in row.ItemArray)
{
mailBody += "<td style='color:blue;'>" + cell.ToString() + "</td>";
}
mailBody += "</tr>";
}
//fill your recipients from your mail list...
foreach (var mail in mailsToSend)
{
mailItem.Recipients.Add(mail);
}
mailBody += "</table>";
mailItem.HTMLBody = mailBody;
//Send your mail....
}
<强>更新强>
只需要7个细胞......
foreach (var cell in row.ItemArray.Take(7))
{
mailBody += "<td style='color:blue;'>" + cell.ToString() + "</td>";
}