股票代码和数据集datagrid视图c#

时间:2012-01-11 22:27:32

标签: c# winforms

我修改了代码,但我仍然遇到麻烦,一切都很好。除非我将数据修改为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");
        }


    }
}  

2 个答案:

答案 0 :(得分:0)

我认为这可能是问题,因为你在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

}

答案 1 :(得分:0)

看起来你只是每秒更新一列。将数据表作为表单的属性并每秒更新一次可能更有效(即,不需要重置网格上的数据源...这可能会导致您遇到问题,因为它会激活很多当你这样做的事件)。

根据我的评论,看起来您只需要在文件系统观察程序事件触发时重新加载数据表,这应该是您重新绑定到网格的唯一时间。

在回复您的评论时,您的代码应如下所示:

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语句比你做的更容易)