简体   繁体   English

如何在Windows窗体中动态更新DataGridView?

[英]How to Update DataGridView dynamically in Windows Forms?

I am extremely sorry if a duplicate/similar question exists. 如果存在重复/类似的问题,我感到非常抱歉。

I have two forms: Result who's output is just a DataGridView and Main_Form which computes values for the Result form. 我有两种形式:Result的输出只是一个DataGridView和Main_Form,它为Result表单计算值。 The input is via a 'folder' which has various files which, after undergoing some computation, generate some values. 输入是通过“文件夹”进行的,该文件夹具有各种文件,这些文件经过一些计算后会生成一些值。 These values are supposed to be put in the DataGridView. 这些值应该放在DataGridView中。

My problem is that the 'folder' had those files being generated dynamically via some other application. 我的问题是,“文件夹”具有那些通过其他应用程序动态生成的文件。 How to I update my DataGridView without creating a new instance of the Result form? 如何在不创建Result表单的新实例的情况下更新DataGridView?

I am creating a new instance of the Result form, so currently my code looks like this. 我正在创建一个Result表单的新实例,因此当前我的代码如下所示。 I would break the infinite loop with after a particular period of time has lapsed...say 20 minutes. 经过一段特定的时间后,我将打破无限循环……比如说20分钟。

while(true)
{
   FileInfo fi = new FileInfo(path);
   if (Directory.EnumerateFiles(path).Any())
   {
       System.IO.DirectoryInfo d = new DirectoryInfo(path);
       FileInfo[] Files = d.GetFiles("*.*");
       foreach (FileInfo f in Files)
       {
           if (IsFileLocked(f)) // since files are generated run-time
              System.Threading.Thread.Sleep(500); // wait                         
           // code that computes values for currently f existing files
           frm = new Result(parameters);
           frm.Show();
   }
   else
      System.Threading.Thread.Sleep(10000);
}

So the issue is knowing when to update the DataGridView ? 所以问题是知道何时更新DataGridView If you are getting the data from files you can use FileSystemWatcher to detect file changes and update the DataGridView . 如果要从文件中获取数据,则可以使用FileSystemWatcher来检测文件更改并更新DataGridView I do not see the form code so you if you want to refresh the grid without creating a new instance you will need to detect the changes in the Result form and update there. 我看不到表单代码,因此如果您要刷新网格而不创建新实例,则需要检测Result表单中的更改并在那里进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM