如何在C#Windows应用程序中通过计时器控件刷新Gridview? [英] How to Refresh a Gridview by a Timer Control in C# Windows Application?

查看:215
本文介绍了如何在C#Windows应用程序中通过计时器控件刷新Gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,在组合框中有多个选项,如5秒,10秒20秒等,当我选择任何一个选项gridview刷新后的特定时间。
以下是在datagridview中加载文件的代码。

I have a combobox and in the combobox I have multiple options like 5sec, 10sec 20 sec, etc, when I select any one option the gridview refreshes after that specific time. Following is code which load files in datagridview.

public string Path { get; set; }
private void UploadButton_Click(object sender, EventArgs e)
{
    var o = new OpenFileDialog();
    o.Multiselect = true;
    if(o.ShowDialog()== System.Windows.Forms.DialogResult.OK)
    {
        o.FileNames.ToList().ForEach(file=>{
            System.IO.File.Copy(file, System.IO.Path.Combine(this.Path, System.IO.Path.GetFileName(file)));
        });
    }

    this.LoadFiles();
}

private void Form_Load(object sender, EventArgs e)
{
    this.LoadFiles();
}

private void LoadFiles()
{
    this.Path = @"d:\Test";
    var files = System.IO.Directory.GetFiles(this.Path);
    this.dataGridView1.DataSource = files.Select(file => new { Name = System.IO.Path.GetFileName(file), Path = file }).ToList();
}

推荐答案

请按照以下步骤操作:


  1. c $ c> 表格

  2. 将$ 5,10加入您的 ComboBox 并将其 DropDownStyle 属性设置为 DropDownList

  3. 处理加载事件表单

  4. 处理 SelectedIndexChanged 事件

  5. 处理 c $ c> 计时器的事件

  1. Put a Timer on your Form
  2. Add 5, 10, 20 to your ComboBox and set its DropDownStyle property to DropDownList
  3. Handle Load event of Form
  4. Handle SelectedIndexChanged event of ComboBox
  5. Handle Tick event of Timer

写入代码:

private void Form1_Load(object sender, EventArgs e)
{
    //Setting 0 as selected index, makes SelectedIndexChanged fire
    //And there we load data and enable time to do this, every 5 seconds
    this.comboBox1.SelectedIndex = 0; //To load each 5 seconds
}

private void LoadFiles()
{
    //Load Data Here
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.timer1.Stop();
    this.LoadFiles(); //optional to load data when selected option changed
    this.timer1.Interval = Convert.ToInt32(this.comboBox1.SelectedItem) * 1000;
    this.timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.LoadFiles();
}

这篇关于如何在C#Windows应用程序中通过计时器控件刷新Gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆