C#.NET中未将对象引用设置到对象的实例 datagridview

来源:百度知道 编辑:UC知道 时间:2024/06/17 04:45:52
代码如下:

private void update_Click(object sender, EventArgs e)
{
for (int i = 0; i <this.dataGridView1.Rows.Count; i++)
{
int stuNo = int.Parse(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
string stuName = this.dataGridView1.Rows[i].Cells[1].Value.ToString();//未将对象引用设置到对象的实例
string stuSex = this.dataGridView1.Rows[i].Cells[2].Value.ToString();
int stuAge = int.Parse(this.dataGridView1.Rows[i].Cells[3].Value.ToString());
string stuID = this.dataGridView1.Rows[i].Cells[4].Value.ToString();
string stuAddress = this.dataGridView1.Rows[i].Cells[5].Value.ToString();
string sql = "update stuInfo set stuName='" + stuName + "',stuSex='" + stuSex + "',stuAge="+stuAge+",stuID='" + stuID + "',stuAddress='" + stuAddress + "' where stuNo

如果这没错的话int stuNo = int.Parse(this.dataGridView1.Rows[i].Cells[0].Value.ToString()); 那么string stuName = this.dataGridView1.Rows[i].Cells[1].Value.ToString();这句话的Cells[1]这出错,你看看这里

获取的变量不对,请仔细检查

未将对象引用到对象的实例一般都是没把对象给new出来..
dataGridView1这个是空间命名吗?真确吗?
如果后面的也对的话.估计是这个string stuName 你检查下..有没初始化..

this.dataGridView1.Rows[i].Cells[1].Value.ToString()
出错信息看是,此处无实例化对象,从你的for (int i = 0; i<this.dataGridView1.Rows.Count; i++)
可以排除没有i行的可能,
那么既然没有i行错误的可能,就是`1列的错误了,你看看是不是有第二列这个东西,自己可以这么试试,调试器中试试this.dataGridView1.Column.Count的数量看看多少,只有大于2才行