我创建了一个应用程序,当电子邮件成功发送时,它会更新日志表单。我的代码类似于以下内容:
mailSender.cs
void Serche()
{
{
//perform thread background ip scanner
}
if (InvokeRequired){
this.Invoke(new MethodInvoker(delegate
{
sendReport();
}));
}
}
public void sendReport()
{
//some codes to trigger time schedule to send report
ExportToExcel(filePath);
int milliseconds = 2000;
Thread.Sleep(milliseconds);
sendMail(filename);
}
private void sendMail(string filename)
{
string getFilePath = @"D:\Report\" + filename;
string status = "send";
try
{
// send email filename as attachment
}
catch (Exception ex)
{
status = "Fail";
}
sendMailReport(filename, DateTime.Now, mailStat);
}
private void sendMailReport(string fileName, DateTime dateDelivered, string status)
{
//mailLog updateLogs = new mailLog();
updateLogs.updateMailLogs(fileName,dateDelivered,status);
}
mailLog.cs
public void updateMailLogs(string _fileName, DateTime _dateDelivered, string _status)
{
int num = dataGridView1.Rows.Add();
dataGridView1.Rows[num].Cells[0].Value = _fileName;
dataGridView1.Rows[num].Cells[1].Value = _dateDelivered;
dataGridView1.Rows[num].Cells[2].Value = _status;
dataGridView1.Refresh();
}
我已经逐行调试代码,并发现所有参数都成功地在我的updateMailLogs方法中检索,但不确定为什么它没有更新我的datagridview。有人知道原因吗?请建议。 解决方案 感谢@shell为我解答了这个问题。
问题: 1- 如果表单已经打开,则无法创建mailLog表单的另一个对象并调用updateMailLogs方法。 2- 这将不会更新您的网格数据。 因为两个对象引用是不同的。
解决方案: 1- 需要从当前加载的mailLog表单对象调用该方法。
private void sendMailReport(string fileName, DateTime dateDelivered,string status)
{
if (Application.OpenForms["mailLog"] != null)
((mailLog)Application.OpenForms["mailLog"]).updateLogs.updateMailLogs(fileName,dateDelivered,status);
}
dataGridView1.Rows.Add(_fileName, _dateDelivered, _Status);
,是否会有任何不同?[我从未必须以这种方式手动刷新...] - Austin T French