我把查询的内容显示在dataGrid里,然后我对选中的某一行进行删除,但是当我删除完所有行时,我再点删除,就会出现错误,错误为:“其他信息:索引0处没有值”,并且指向代码中的这一行:“string Stop_ID = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString();”,不知道怎么搞,在没有内容时可以提示错误,不采用try...catch!

解决方案 »

  1.   

    :“string Stop_ID = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString();”,
    你删除的时候,是用的按钮吧。
    当datagrid里面没有数据之后,再点击按钮,那么this.dataGrid1.CurrentCell.RowNumber就会出错。
      

  2.   

    如果你的数据源是用的datatable,那么你就这样:
    datatable dt=(datatable)dataGrid.DataSource;
    if (dt.rows.count==0)//说明没有记录了。
    {
    return;
    }如果你的用其它数据源,也可以通过这种原理来知道是否有记录。
      

  3.   

    dataGrid1.CurrentRowIndex=-1时,说明dataGrid里面没数据。
    这样最好用。
      

  4.   

    if (dataGrid1.CurrentRowIndex==-1)//说明没数据
    {
    return;
    }