C# DataGridVIew行到电子邮件

C# DataGridVIew行到电子邮件,c#,datagridview,C#,Datagridview,我有DataGridView控件,希望每个选定行中的单元格值作为电子邮件正文文本发送为一个字符串行: private void buttonReport_Click(object sender, EventArgs e) { MailAddress from = new MailAddress("hi@greetings.com", "Hello"); MailAddress to = new MailAddress("hi1234@gma

我有DataGridView控件,希望每个选定行中的单元格值作为电子邮件正文文本发送为一个字符串行:

        private void buttonReport_Click(object sender, EventArgs e)
    {
        MailAddress from = new MailAddress("hi@greetings.com", "Hello");
        MailAddress to = new MailAddress("hi1234@gmail.com", "Hi");
        string join;

        foreach (DataGridViewRow row in dgvTest.Rows)
        {
            List<string> email = new List<string>();
            {
                if (Convert.ToBoolean(row.Cells[cbSelect.Name].Value) == true)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value != null)
                        {
                            email.Add(cell.Value.ToString());
                        }
                    }
                }
            }
            foreach (string items in email.Skip(1).Take(4))
            {
                join = String.Join(", ", items.ToString());
                SendEmail("Hi!", from, to, join);
            }
        }
    }
private void按钮报告\单击(对象发送者,事件参数e)
{
MailAddress from=新邮件地址(“hi@greetings.com","你好",;
MailAddress to=新邮件地址(“hi1234@gmail.com","嗨",;
串接;
foreach(dgvTest.Rows中的DataGridViewRow行)
{
列表电子邮件=新列表();
{
if(Convert.ToBoolean(row.Cells[cbSelect.Name].Value)==true)
{
foreach(row.Cells中的DataGridViewCell单元格)
{
if(cell.Value!=null)
{
email.Add(cell.Value.ToString());
}
}
}
}
foreach(电子邮件中的字符串项。跳过(1)。接受(4))
{
join=String.join(“,”,items.ToString());
发送电子邮件(“嗨!”,发件人,收件人,加入);
}
}
}

这是代码,但是我现在收到4封邮件,因为
SendMail()
在foreach循环中,如果我将其取出,则无法访问局部变量join。。?如何解决这个问题?我正在使用
电子邮件。跳过(1)。使用(4)
忽略一些不必要的值?提前感谢

移动声明
列表电子邮件=新列表()外部foreach。不确定为什么需要跳过(1)。选择(4)

我建议使用以下代码:

//declared somewhere above
List<string> CellsForReport = new List<string>{ "item1", "item2", "item3", "item4" }

//code inside buttonReport_Click
List<string> emails = new List<string>();
foreach (DataGridViewRow row in dgvTest.Rows)
{
  if (Convert.ToBoolean(row.Cells[cbSelect.Name].Value) == true)
  {
    //list to collect current row items
    var currentMailItems = new List<string>()
    foreach (var cellName in CellsForReport )
    {
       //this can throw exception if you specified wrong name above
       var cell = row.Cells[cellName];
       if (cell.Value != null)
       {
         currentMailItems.Add(cell.Value.ToString());
       }
    }
    //construct email text from row
    var finalEmailText = string.Join(", ", currentMailItems);
    emails.Add(finalEmailText);
  }
}
//sending 1 email per row
foreach (var email in emails)
{
  SendEmail("Hi!", from, to, email);
}
//在上面某处声明
List CellsForReport=新列表{“item1”、“item2”、“item3”、“item4”}
//内部代码按钮报告\单击
列表电子邮件=新列表();
foreach(dgvTest.Rows中的DataGridViewRow行)
{
if(Convert.ToBoolean(row.Cells[cbSelect.Name].Value)==true)
{
//用于收集当前行项目的列表
var currentMailItems=新列表()
foreach(CellsForReport中的变量cellName)
{
//如果您在上面指定了错误的名称,则可能引发异常
var cell=row.Cells[cellName];
if(cell.Value!=null)
{
currentMailItems.Add(cell.Value.ToString());
}
}
//从行构造电子邮件文本
var finalEmailText=string.Join(“,”,currentMailItems);
电子邮件。添加(finalEmailText);
}
}
//每行发送1封电子邮件
foreach(电子邮件中的var电子邮件)
{
发送电子邮件(“嗨!”,发件人,收件人,电子邮件);
}

您的建议有效,谢谢您,正如我最初所说的,我希望整行只有4个值,而不是所有值,我怎么能特别选择item1、item2、item3和item4并将它们作为电子邮件正文发送?@y4wn请参阅我的编辑:指定真实的单元格名称,而不是
“item1”、“item2”、“item3”、“item4”
确定,我已经找到了如何做
var finalEmailText=string.Join(“,”,currentMailItems[1],currentMailItems[2],currentMailItems[3],currentMailItems[4])。非常感谢!