c# - Datagridview 未在已打开的表单上更新

标签 c# winforms datagridview

我创建了一个应用程序,只要电子邮件成功发送,它就会更新日志表单。我的代码是这样的:

ma​​ilSender.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);
    }

ma​​ilLog.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);
}

最佳答案

提供的代码无助于准确理解您所做的事情。我的意思是您正在执行方法 sendMailReport。该方法将在每次执行时创建 mailLog 类的对象。这可能会丢失您现有的数据。最好在 sendMailReport 方法 block 之外创建 mailLog 类对象,并且只执行 updateMailLogs 方法。

mailLog updateLogs = new mailLog();
private void sendMailReport(string fileName, DateTime dateDelivered,string status)
{        
    updateLogs.updateMailLogs(fileName,dateDelivered,status);
}

已编辑:

如果表单已经加载,那么你应该像这样调用方法。在这里,您不需要创建一个新的 mailLog 类对象。

private void sendMailReport(string fileName, DateTime dateDelivered,string status)
{        
    ((mailLog)Application.OpenForms["mailLog"]).updateMailLogs(fileName,dateDelivered,status);
}

关于c# - Datagridview 未在已打开的表单上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601744/

相关文章:

C# 如何提交一个文本框?

c# - C# Windows 窗体中的状态栏

c# - 如何处理 DataGridViewCell 中的 KeyEvents?

.net - 如何连续循环DataGridView行?

c# - 在长时间运行的 Hangfire 进程中发送心跳

c# - 将错误代码大量转换为异常

c# - .NET 将 SSL 证书添加到 SOAP 客户端请求

c# - 如何在 Windows 上实现桌面叠加模糊?

c# - 如何/在哪里存储当前用户的登录名?

c# - Winforms 中的 DatagridView,SqlDataReader 是否在循环结束时自动关闭?