这个程序功能: 
先讲数据库的数据放入datasete中 然后在datasate中新建一个datatable 通过timer 每次加入7条信息,然后在DataGridView中显示出来,每5秒钟显示其中7条信息,如果到最后7条信息 显示完之后就从0开始。报错现象:
当timer执行第二次或者第三次时,就出现报错。
报错内容是:标题:DataGridView默认错误对话框,
            System.IndexOutOfRangeException索引0没有值。
            在System.Windows.Froms.CurrencyManager.get_Item(Int32 index)
          在System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)以上是报错内容
下面是代码
谢谢大家了!!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Timers;namespace datasat_text
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            a = 0;
            b = 0;
            c = 0;
            con(null,null);
        }        SqlConnection cn;
        SqlCommand cmm;
        SqlDataAdapter da;
        DataSet ds;
        DataTable dt;
        System.Timers.Timer ti;
        DataRow dr;
        public int a = 0;
        public int b = 0;
        public int c = 0;
        public int count = 0;        public void con(object sender, EventArgs e)
        {
            cn = new SqlConnection();
            cn.ConnectionString = @"server = . ; database = aa ; Integrated Security=SSPI";
            cn.Open();
            cmm = new SqlCommand();
            cmm.CommandText = "select * from 供应商";
            da = new SqlDataAdapter(cmm.CommandText, cn);
            ds = new DataSet();
            da.Fill(ds, "infor");
            cn.Close();            dt = new DataTable("temp");
            DataColumn dc1 = new DataColumn("city", typeof(float));
            DataColumn dc2 = new DataColumn("name", typeof(string));
            DataColumn dc3 = new DataColumn("car_number", typeof(string));            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            ds.Tables.Add(dt);            count = ds.Tables["infor"].Rows.Count ;
                   }        public void add(object sender, EventArgs e)
        {
            ds.Tables["temp"].Clear();            
            if(a+7 < count)
            {
                c = a;
                a += 7;
                for (int i = c; i < a; i++)
                {
                    dr = ds.Tables["temp"].NewRow();
                    dr["city"] = ds.Tables["infor"].Rows[i][0];
                    dr["name"] = ds.Tables["infor"].Rows[i][1];
                    dr["car_number"] = ds.Tables["infor"].Rows[i][2];
                    dt.Rows.Add(dr);
                }
            }
            else
            {
                c = a;
                for (int i = c; i < count; i++)
                {
                    dr = ds.Tables["temp"].NewRow();
                    dr["city"] = ds.Tables["infor"].Rows[i][0];
                    dr["name"] = ds.Tables["infor"].Rows[i][1];
                    dr["car_number"] = ds.Tables["infor"].Rows[i][2];
                    dt.Rows.Add(dr);
                }
                a = 0;
                b = 0;
                c = 0;
            }
            this.dataGridView1.DataSource = ds.Tables["temp"];            
        }        private void button1_Click(object sender, EventArgs e)
        {
            ti = new System.Timers.Timer(5000);
            ti.Elapsed += new ElapsedEventHandler(add);
            ti.Start();            
        }
    }
}

解决方案 »

  1.   

    补充一下 这个程序是 c# winform
      

  2.   

    我做了一下,没有任何错误,我用的Timer是控件提供的。下面是代码,直接赋值应该就可以了。       #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.button1 = new System.Windows.Forms.Button();
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.timer1 = new System.Windows.Forms.Timer(this.components);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(21, 248);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Location = new System.Drawing.Point(21, 27);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(398, 198);
                this.dataGridView1.TabIndex = 1;
                // 
                // timer1
                // 
                this.timer1.Interval = 5000;
                this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(581, 290);
                this.Controls.Add(this.dataGridView1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.Timer timer1;   public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                a = 0;
                b = 0;
                c = 0;
                con(null, null);
            }        SqlConnection cn;
            SqlCommand cmm;
            SqlDataAdapter da;
            DataSet ds;
            DataTable dt;
            DataRow dr;
            public int a = 0;
            public int b = 0;
            public int c = 0;
            public int count = 0;        public void con(object sender, EventArgs e)
            {
                cn = new SqlConnection();
                cn.ConnectionString = @"server = B321451588E441A\SQL2005 ; database = Northwind ;uid=sa;pwd=111";
                cn.Open();
                cmm = new SqlCommand();
                cmm.CommandText = "select * from Employees";
                da = new SqlDataAdapter(cmm.CommandText, cn);
                ds = new DataSet();
                da.Fill(ds, "infor");
                cn.Close();            //加入另外一张表
                dt = new DataTable("temp");
                DataColumn dc1 = new DataColumn("City", typeof(string));
                DataColumn dc2 = new DataColumn("LastName", typeof(string));
                DataColumn dc3 = new DataColumn("FirstName", typeof(string));            dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);            ds.Tables.Add(dt);            count = ds.Tables["infor"].Rows.Count;
            } 
            private void button1_Click(object sender, EventArgs e)
            {
                timer1.Start();            
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                ds.Tables["temp"].Clear();
                if (a + 7 < count)
                {
                    c = a;
                    a += 7;
                    for (int i = c; i < a; i++)
                    {
                        dr = ds.Tables["temp"].NewRow();
                        dr["City"] = ds.Tables["infor"].Rows[i][0];
                        dr["LastName"] = ds.Tables["infor"].Rows[i][1];
                        dr["FirstName"] = ds.Tables["infor"].Rows[i][2];
                        dt.Rows.Add(dr);
                    }
                }
                else
                {
                    c = a;
                    for (int i = c; i < count; i++)
                    {
                        dr = ds.Tables["temp"].NewRow();
                        dr["City"] = ds.Tables["infor"].Rows[i][0];
                        dr["LastName"] = ds.Tables["infor"].Rows[i][1];
                        dr["FirstName"] = ds.Tables["infor"].Rows[i][2];
                        dt.Rows.Add(dr);
                    }
                    a = 0;
                    b = 0;
                    c = 0;
                }
                this.dataGridView1.DataSource = ds.Tables["temp"];
            }     }
      

  3.   

        我试了试 还是会出现想我前面说的报错内容。不知道为何?     而且第一次不会报错 , 第二次就出现了 ,但是 datagridview里的数据是有变化的,但是老是跳出来那个错误对话框。
      
        
      

  4.   

    我说让吧System.Timers.Timer ti;  换成System.Windows.Form.Timer.可以从工具箱中拖拽的。
    如果已经改了的话。
    我想知道是哪一句话产生的异常?
    可以将异常产生的那条语句用Try Catch处理一下try
    {
    //异常产生的语句
    }
    catch(Exception ex)
    {
    }这样应该就不会跳出错误对话框了。
      

  5.   

    你指定DataGridView的连接对象了吗?如果没有的话,就是胡乱的探出错误信息
      

  6.   

    恩  恩   今天下午我又试了一次 换了一台电脑 结果没有出现报错现象!!是不是我本机的系统的关系?如果是的话 就像4楼的朋友说的 用try 和 catch 就能解决那个问题吗? 不好意思 下午去上课了 所以没有回复!!