2013-06-03 61 views
0

我有一个代码,将通过电子邮件发送在datagridview上可见的所有。我希望它在将所有人的姓名放入电子邮件到部分之前删除列为“开放职位”的任何人。C#删除如果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); 

    } 
} 

回答

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

如何能够删除列表中名称为“打开”的任何人?谢谢! –

+0

完美!最后一件事..如果我想要C3,C4 ..我会怎么结合2件以上的东西?谢谢!! –

+0

'c1.Union(c2).Union(c3).....'like that – Damith

相关问题