ticker和数据集datagrid视图c#
本文关键字:视图 datagrid 数据集 ticker | 更新日期: 2023-09-27 18:19:59
我修改了代码,但我仍然有麻烦,一切都很好。除非我将数据修改到XML文件中,否则应用程序会崩溃。当我将数据修改到xml文件中时,应该刷新datagridview。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Threading;
using System.Reflection;
namespace XML
{
public partial class Form1 : Form
{
DataSet formBindingSource = null;
public Form1()
{
InitializeComponent();
//
formBindingSource = new DataSet();
using (FileStream stream1 = new FileStream("c:''sites.xml", FileMode.Open))
{
formBindingSource.ReadXml(stream1);
}
this.UpdateDataGrid();
dataGridView1.DataSource = formBindingSource.Tables[0];
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
FileSystemWatcher incoming = new FileSystemWatcher();
incoming.Path = @"c:'";
incoming.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName;
incoming.Filter = "sites.xml";
incoming.Changed += new FileSystemEventHandler(OnChanged);
incoming.EnableRaisingEvents = true;
//
//
}
public void OnChanged(object source, FileSystemEventArgs e)
{
using (FileStream stream1 = new FileStream("c:''sites.xml", FileMode.Open))
{
formBindingSource.ReadXml(stream1);
}
this.UpdateDataGrid();
dataGridView1.DataSource = formBindingSource.Tables[0];
}
public void UpdateDataGrid()
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { UpdateDataGrid(); });
}
else
{
//refresh column status evry second
int count = 0;
foreach (DataRow dr in formBindingSource.Tables[0].Rows)
{
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(dr[0]);
DateTime EndTime = Convert.ToDateTime(dr[1]);
if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
formBindingSource.Tables[0].Rows[count][5] = "ok";
}
else
{
formBindingSource.Tables[0].Rows[count][5] = "nok";
}
count++;
}
formBindingSource.Tables[0].DefaultView.RowFilter = "date = #" + DateTime.Today + "#";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
this.UpdateDataGrid();
this.label1.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy hh:mm:ss tt");
}
}
}
看起来您只是每秒更新一列。使数据表成为表单的一个属性并每秒更新一次可能会更有效(即,不需要重置网格上的数据源……这可能会给您带来问题,因为这样做会引发很多事件)。
根据我的评论,看起来你只需要在文件系统观察程序事件触发时重新加载数据表,这应该是你重新绑定到网格的唯一时间。
作为对您评论的回应,您的代码应该如下所示:
namespace XML
{
public partial class Form1 : Form
{
DataSet formBindingSource = null;
public Form1()
{
InitializeComponent();
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
FileSystemWatcher incoming = new FileSystemWatcher();
incoming.Path = @"c:'";
incoming.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName;
incoming.Filter = "sites.xml";
incoming.Changed += new FileSystemEventHandler(OnChanged);
incoming.EnableRaisingEvents = true;
}
public void OnChanged(object source, FileSystemEventArgs e)
{
formBindingSource = new DataSet();
using(FileStream stream1 = new FileStream("c:''sites.xml", FileMode.Open))
{
ds.ReadXml(stream1);
}
this.UpdateDataGrid();
dataGridView1.DataSource = formBindingSource.Tables[0];
}
public void UpdateDataGrid()
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { UpdateDataGrid(); });
}
else
{
//refresh column status evry second
int count = 0;
foreach (DataRow dr in formBindingSource.Tables[0].Rows)
{
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(dr[0]);
DateTime EndTime = Convert.ToDateTime(dr[1]);
if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
ds.Tables[0].Rows[count][5] = "ok";
}
else
{
ds.Tables[0].Rows[count][5] = "nok";
}
count++;
}
formBindingSource.Tables[0].DefaultView.RowFilter = "date = #" + DateTime.Today + "#";
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Load and bind file
OnChanged(null,null)
}
private void timer1_Tick(object sender, EventArgs e)
{
this.UpdateDataGrid();
this.label1.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy hh:mm:ss tt");
}
}
}
请注意如何存在一个名为formBindingSource的表单级数据集。当您更新时,您的网格应该自动更新,而不必重置网格的数据源。您只需要在文件更改并加载新数据集时重新绑定。
(此外,在文件流代码中使用using
语句比您所做的更容易)
我认为这可能是问题所在,因为在for循环中更改DataGridView的数据源,什么不是最好的方法:
foreach (DataRow dr in ds.Tables[0].Rows)
{
String StartCourse = dr[0].ToString();
string EndCourse = dr[1].ToString();
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(StartCourse);
DateTime EndTime = Convert.ToDateTime(EndCourse);
if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
ds.Tables[0].Rows[count][5] = "ok";
}
else
{
ds.Tables[0].Rows[count][5] = "nok";
}
count++;
//dataGridView1.DataSource = ds.Tables[0]; <- HERE COULD BE THE PROBLEM
}