我在窗体中 放置一个 dataGridView控件 连接SQL server2005 报错!

来源:百度知道 编辑:UC知道 时间:2024/05/06 12:18:54
我在窗体中 放置一个 dataGridView控件 连接SQL server2005 ,
已经连上 可以添加元素 但是在
int delstudentID=int.parse(dataGridView1.selectedRow[0].cell[0].value.tostring());时程序报错说是“索引必须是非负值或小于最大值”
delstID是要删除的学号那行

dataGridView1没有数据或选择的行为空。加个判断:
int delstudentID = 0;
if(dataGridView1.selectedRows.Count>0)
delstudentID=int.parse(dataGridView1.selectedRows[0].cell[0].value.tostring());
else
MessageBox.show("未选择任何行!");

dataGridView1是空的,你Fill时就有问题了,看一下代码