求救:dataGridView绑定动态变化的数据出错
做了一个简单的考试软件
一个简单的考试软件(C/S)
//考生数据库装入
OleDbConnection myConnection = new OleDbConnection(@ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + databaseFileName);
DataSet myDataSet = new DataSet();
OleDbDataAdapter myOleDbDataAdapter = new OleDbDataAdapter( " SELECT * FROM TestTable ", myConnection);
myOleDbDataAdapter.Fill(myDataSet, "TestTable ");
//设置myDataSet的主键
DataColumn[] keys = new DataColumn[1];
keys[0] = myDataSet.Tables[ "TestTable "].Columns[ "考号 "];
myDataSet.Tables[ "TestTable "].PrimaryKey = keys;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = myDataSet;
dataGridView1.DataMember = "TestTable ";
//在dataGridView中用右键菜单简单筛选,考试状态字段,剩余时间字段在动态变化
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
Monitor.Enter(this);//多个线程读或写myDataSet,控制同步访问
DataView dv = myDataSet.Tables[0].DefaultView;
if(ToolStripMenuItem1==(ToolStripMenuItem)sender)
dv.RowFilter = "考试状态= '正在考试 ' ";//筛选正在考试的记录
else if(ToolStripMenuItem2==(ToolStripMenuItem)sender)
dv.RowFilter = "考试状态= '考试完毕 ' ";
else if(ToolStripMenuItem3==(ToolStripMenuItem)sender)
dv.RowFilter = "考试状态= '网络错误 ' ";
else if(ToolStripMenuItem4==(ToolStripMenuItem)sender)
dv.RowFilter = "考试状态 LIKE '* ' ";
this.dataGridView1.DataSource = dv;
Monitor.Exit(this);
}
不用筛选内容填充dataGridView1时,显示没问题
但选 "正在考试 "筛选后,有的学生记录从 "正在考试 "变成 "考试完毕 "(在一个timer中修改myDataSet)后退出当前视图时出错:
The following exception occurred in the DataGridView:
System.IndexOutOfRangeException:Index 5 does not have a value,
at System.Windows.Forms.CurrencyManger.get_Item(Int32 index)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)