如何在C#Windows应用程序中通过计时器控件刷新Gridview? [英] How to Refresh a Gridview by a Timer Control in C# Windows Application?
本文介绍了如何在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();
}
推荐答案
请按照以下步骤操作:
- c $ c>
表格
- 将$ 5,10加入您的
ComboBox
并将其DropDownStyle
属性设置为DropDownList
- 处理
加载
事件
表单
- 处理
SelectedIndexChanged
事件
- 处理
c $ c>
计时器的事件
- Put a
Timer
on yourForm
- Add 5, 10, 20 to your
ComboBox
and set itsDropDownStyle
property toDropDownList
- Handle
Load
event ofForm
- Handle
SelectedIndexChanged
event ofComboBox
- Handle
Tick
event ofTimer
写入代码:
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屋!
查看全文