这个程序功能:
先讲数据库的数据放入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();
}
}
}
先讲数据库的数据放入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();
}
}
}
解决方案 »
- 关于无限制商品分类。。。
- SQL Server 2008 r2与VS 2010 的连接问题
- 用ODBC连接DBF文件SQL语句检索条件不支持"DATEDIFF(DateValue(VALUE),Now(),'d')"求解决办法
- 请问各位前辈怎么样才能我的写程序更加的美观???
- 怎样使用Web.config定义的网页的标题(急急急)
- 请问C#和JAVA哪个更有前途
- 控件类RichControl在那里啊?
- 急!!如何将DIK0HK@`1p转化为汉字呢?
- 多个窗体中如何显示同一个控件?
- 一个页面中的按钮事件怎样访问其他页面中的对象?
- 使用C#中的OracleClient遇到的一个奇怪问题
- 关于用C#编游戏
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </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"];
} }
如果已经改了的话。
我想知道是哪一句话产生的异常?
可以将异常产生的那条语句用Try Catch处理一下try
{
//异常产生的语句
}
catch(Exception ex)
{
}这样应该就不会跳出错误对话框了。