Convert.ToString(GridView1.DataKeys[e.RowIndex].Values[0])当他的值为空时会报错索引超出范围。必须为非负值并小于集合大小。
参数名: index 请问如何判断他的值是否为空?        string cartid;
        if (Convert.ToString(GridView1.DataKeys[e.RowIndex].Values[0]) == null)
            cartid = Convert.ToString(GridView1.DataKeys[e.RowIndex].Values[0]);
        else
            cartid = "";我要完成的结果因为刷新页面后被索引的数据已经删除了,所以取不到索引值了。请问如何判断GridView1.DataKeys里索引值是否为空呢?

解决方案 »

  1.   

    GridView1.DataKeys[e.RowIndex].Values[0] == null
      

  2.   

    查看一下设计页面,是不是DATAKEYNAME没有设置,
    还有,楼上说的也要注意一下,为NULL的对象怎么Convert
      

  3.   

    可以试试判断一下以下的
            if (GridView1.DataKeys.Count==0)
            {         }
      

  4.   


    去掉Convert.ToString值是不转成string了,视乎没有什么意义啊。
    而且这个我也试过的,不好用。
      

  5.   

    判断GridView1.DataKeys.Count是否为空