我有一个代码,可以通过电子邮件发送datagridview上可见的所有内容。我希望它删除任何列为“未结头寸”的人,然后将所有人的姓名放入电子邮件TO部分。
任何帮助将不胜感激!
这是我到目前为止的代码....
try
{
String str = "", str1 = "";
for (int i = 0; i < dataGridView1.RowCount; i++)
{
str1 = dataGridView1.Rows[i].Cells["C1"].Value.ToString();
if (!str.Contains(str1)) str += str1 + ";";
str1 = dataGridView1.Rows[i].Cells["C2"].Value.ToString();
if (!str.Contains(str1)) str += str1 + ";";
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem);
outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItem.To = str;
mailItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceLow;
mailItem.Display(false);
}
}
答案 0 :(得分:0)
使用Linq
// get c1 cell values to a list
var c1 = dataGridView1.Rows.Cast<DataGridViewRow>()
.Select(row => row.Cells["C1"].Value.ToString()).ToList();
// get c2 cell values to a list
var c2 = dataGridView1.Rows.Cast<DataGridViewRow>()
.Select(row => row.Cells["C2"].Value.ToString()).ToList();
// remove duplicates and join them and assign
mailItem.To = string.Join(";", c1.Union(c2).ToArray());
如果您有任何其他列值决定选择单元格值,您只需添加条件和过滤器记录,如下所示
// get c1 cell values to a list
var c1 = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(r=>r.Cells["Name"].Value.ToString() != "open")
.Select(row => row.Cells["C1"].Value.ToString()).ToList();