0

我有一个代码,它将通过电子邮件发送 datagridview 上可见的所有内容。我希望它在将每个人的姓名放在电子邮件收件人部分之前删除任何被列为“空缺职位”的人。

任何帮助,将不胜感激!

这是我到目前为止的代码....

 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);

     }
 }
4

1 回答 1

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());

如果您有任何其他列值决定选择单元格值,您可以简单地添加 where 条件和过滤记录,如下所示

// 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();
于 2013-06-03T09:43:04.630 回答