C#:更新数据网格

发布于 2024-08-31 15:21:17 字数 186 浏览 6 评论 0原文

我有一个带有 Windows 窗体数据网格的 C# 应用程序。我需要监视一个目录(正在使用 FileSystemWatcher)并使用目录中的文件列表刷新数据网格。我不确定如何设置界面来做到这一点?从 Windows 窗体 Load() 调用 monitorDirectory() 似乎不起作用,因为 Load 在应用程序中仅调用一次。

谢谢

I have a C# application with a datagrid in windows form. I need to monitor a directory (am using FileSystemWatcher) and refresh the datagrid with list of files in the directory. I am not sure how I can set up the interface to do so? Calling the monitorDirectory() from the windows-form Load() does not seem to work as Load is called only once in the application.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

极致的悲 2024-09-07 15:21:17

您可以在 FileSystemWatcher 的 OnChanged 或 OnRenamed 事件处理程序中更新网格。

下面链接中的示例正在处理控制台应用程序中的事件。
MSDN FileSystemWatcher 类

You could update your grid within the OnChanged or OnRenamed event handlers of your FileSystemWatcher.

The example at the link below is handling the events within a Console application.
MSDN FileSystemWatcher Class

九公里浅绿 2024-09-07 15:21:17

您可以侦听来自 FileSystemWatcher 对象的事件。 MSDN 页面发布了一些有关如何操作的建议这。

本质上,在调用 MonitorDirectory() 之前,您应该订阅 FileSystemWatcher 的 Changed、Created、Deleted 和 Renamed 事件。

You can listen to events from the FileSystemWatcher object. The MSDN page posts some recommendations on how to do this.

Essentially, right before calling MonitorDirectory(), you should subscribe to the Changed, Created, Deleted, and Renamed events of your FileSystemWatcher.

橙幽之幻 2024-09-07 15:21:17

完整代码

将 Form1 添加到您的项目

将 Form1.Designer.cs 替换为

namespace Test
{
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(13, 13);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(240, 150);
        this.dataGridView1.TabIndex = 0;
        // 
        // fileSystemWatcher1
        // 
        this.fileSystemWatcher1.EnableRaisingEvents = true;
        this.fileSystemWatcher1.Path = "c:\\Temp";
        this.fileSystemWatcher1.SynchronizingObject = this;
        this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
        this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
        this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
        this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(370, 301);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.IO.FileSystemWatcher fileSystemWatcher1;
}
}

将 Form1.cs 替换为

using System;
using System.IO;
using System.Windows.Forms;

namespace Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void PopulateGrid()
    {
        DirectoryInfo dir = new DirectoryInfo(fileSystemWatcher1.Path);

        dataGridView1.DataSource = dir.GetFiles();
    }


    private void fileSystemWatcher1_CreatedDeletedChanged(object sender, FileSystemEventArgs e)
    {
        PopulateGrid();
    }



    private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
    {
        PopulateGrid();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        PopulateGrid();
    }
}
}

Full Code

Add a Form1 to your project

Replace Form1.Designer.cs with

namespace Test
{
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(13, 13);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(240, 150);
        this.dataGridView1.TabIndex = 0;
        // 
        // fileSystemWatcher1
        // 
        this.fileSystemWatcher1.EnableRaisingEvents = true;
        this.fileSystemWatcher1.Path = "c:\\Temp";
        this.fileSystemWatcher1.SynchronizingObject = this;
        this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
        this.fileSystemWatcher1.Created += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
        this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_CreatedDeletedChanged);
        this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(370, 301);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.IO.FileSystemWatcher fileSystemWatcher1;
}
}

Replace Form1.cs with

using System;
using System.IO;
using System.Windows.Forms;

namespace Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void PopulateGrid()
    {
        DirectoryInfo dir = new DirectoryInfo(fileSystemWatcher1.Path);

        dataGridView1.DataSource = dir.GetFiles();
    }


    private void fileSystemWatcher1_CreatedDeletedChanged(object sender, FileSystemEventArgs e)
    {
        PopulateGrid();
    }



    private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
    {
        PopulateGrid();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        PopulateGrid();
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文