当前位置:编程学习 > C#/ASP.NET >>

这都报错?怎么回事?dataGridView1的问题


           DataSet ds = dpdal.getAllDepartment();

            if (ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Columns[0].Visible = false;
                textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
                
            }
            else
            {
                return;
            }
  报的是 未将对象引用设置到对象的实例。 

然后我发现改了下dataGridView1.Columns[0].Visible = false;
里面的数字!就不会报错了,例如改成dataGridView1.Columns[3].Visible = false;
就不会报错了,为什么会这样?还有的解决方法是 先拿值出来,再设置这句
dataGridView1.Columns[0].Visible = false;

也不错报错 求原因 --------------------编程问答-------------------- dataGridView1.Columns[0] 不存在。 --------------------编程问答-------------------- 这个真的存在的。。换了个位置就可以了 dataGridView1.Columns[0] 不是第一列? --------------------编程问答-------------------- 因为一共只有4列值,你把其中一列的值隐藏了,所以 cell[3]的值也就报未将对象引用到实例了. --------------------编程问答-------------------- 同意楼上的顶···
--------------------编程问答-------------------- 你的dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value为NULL
.ToString()就出现错误了 --------------------编程问答--------------------
引用 3 楼 windy19871011 的回复:
因为一共只有4列值,你把其中一列的值隐藏了,所以 cell[3]的值也就报未将对象引用到实例了.



+1 --------------------编程问答-------------------- 可以通过调试来查看Columns的个数已经每个column的值。 --------------------编程问答-------------------- 看来高手还是很多啊 --------------------编程问答-------------------- 下面这句引起的。


dataGridView1.Columns[0].Visible = false;
             --------------------编程问答--------------------
引用 3 楼 windy19871011 的回复:
因为一共只有4列值,你把其中一列的值隐藏了,所以 cell[3]的值也就报未将对象引用到实例了.


这个。。。我是说cell[3]的时候不会报错。。。。就是列的数量肯定是大于10的。。
你的理解是cell[3]报错。。。
我是指
dataGridView1.Columns[0].Visible = false; 会报错。
dataGridView1.Columns[3].Visible = false; 不会报错

求解释求解答 --------------------编程问答--------------------
引用楼主 gerrytang 的回复:
C# code

           DataSet ds = dpdal.getAllDepartment();

            if (ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
                ……


改为
DataSet ds = dpdal.getAllDepartment();

            if (ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Columns[3].Visible = false;                textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
                
            }
            else
            {
                return;
            }

仅仅是改一,  就不报错? --------------------编程问答--------------------
引用 11 楼 qq183670101 的回复:
引用楼主 gerrytang 的回复:
C# code

DataSet ds = dpdal.getAllDepartment();

if (ds.Tables[0].Rows.Count != 0)
{
dataGridView1.DataSource = ds.Tables[0];
……


改为
DataSet ds = dpdal.getAllDepartmen……



 DataSet ds = dpdal.getAllDepartment();

            if (ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Columns[1].Visible = false;
                textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
                
            }
            else
            {
                return;
            }
 是的 改成这样就不报错了 ,想让他不报错也实现隐藏 还可以这样,而我现在就是这样

DataSet ds = dpdal.getAllDepartment();

            if (ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
                                textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
                dataGridView1.Columns[0].Visible = false;

            }
            else
            {
                return;
            }
  这样也不报错,想知道原因吖 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,