【问题标题】:Datagridview not updated on form that already openDatagridview 未在已打开的表单上更新
【发布时间】:2014-07-07 00:54:10
【问题描述】:

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

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

【问题讨论】:

  • 如果你这样做有什么不同dataGridView1.Rows.Add(_fileName, _dateDelivered, _Status); [我从来没有用这种方式手动刷新...]
  • @AustinFrench 是的,我已经用那个方法绑定了,但结果还是一样。它没有刷新数据网格视图。
  • 嗯,@Ren 你在尝试吗? dgv 是否正好有 3 列?我从一个带有假属性的按钮中使用了你的方法(复制粘贴),没有问题......
  • @AustinFrench 是的,我已经仔细检查了这些列,而且我正好有 3 个。如果这两个 mailSender.cs 和 mailLog.cs 都是不同的分隔形式,也许会有影响?如果我错了,请纠正我。
  • 应该没问题,但要澄清一下,任何地方都没有 try、catch 或 finally 语句来捕捉这个?

标签: c# winforms datagridview


【解决方案1】:

提供的代码无助于理解您所做的一切。我的意思是你正在执行方法sendMailReport。该方法将在每次执行时创建mailLog 类的对象。这可能会丢失您现有的数据。最好在 sendMailReport 方法块之外创建 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);
}

【讨论】:

  • tq,我已经按照你的建议进行了尝试。我将 mailLog 类置于 Initialize 状态。但 datagridview 仍然没有更新该行。知道为什么吗?
  • 我已经编辑了我的问题,提供一些额外的代码,希望它可能会有所帮助。tq
  • mailLog.cs 是表单还是简单类?我的意思是你把dataGridView1控件放在哪里了?
  • mailLog.cs 是主要形式。我把datagridview放在mailLog.cs的主窗体中
  • 如何显示mailLog 表单?如果表单已经打开,那么您不能创建mailLog 表单的另一个对象并调用updateMailLogs 方法。这不会更新您的网格数据。因为两个对象引用不同。您必须从当前加载的mailLog 表单的对象中调用该方法。
【解决方案2】:

这通常是由于 CrossThread 异常而发生的,所以我想你需要添加 try catch 来检查,如果你需要调用网格

编辑:刚刚注意到你问在哪里放 try catch 你可以把它放在你的两个空隙中,试试这个

try{
     updateLogs.updateMailLogs(fileName,dateDelivered,status);
   }
catch (Exception ex) {MessageBox.Show(ex.ToString());}

【讨论】:

  • 谢谢,我已经按照您的建议编辑了我的代码。但是没有抛出异常。请问有什么办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2014-03-23
  • 1970-01-01
相关资源
最近更新 更多