try
{
  x=ds.Tables["mytbale"].DefaultView.Count;
}
catch(Exception ex)
{
   x=0;
}
但说真,你这是小题大作 了,只要这样写就够了:
x= (ds.Tables["mytbale"].DefaultView.Count)!=null ? (ds.Tables["mytbale"].DefaultView.Count) : 0

解决方案 »

  1.   

    if(ds.Tables["mytbale"].DefaultView.Count!=null)
     x=ds.Tables["mytbale"].DefaultView.Count;
    else
     x=0这样比你用try更快
    你的try 语句错了,应该是
    try
    {可能出错的过程}
    catch(Exception ex)
    {出错时的处理}
      

  2.   

    int x=dataGrid1.CurrentRowIndex;//获得当前行号
    dataGrid1.UnSelect(x);
    if (x<0) x=0;ds.Tables["mytbale"].DefaultView中记录为空时,上的“if (x<0) x=0;”也提示“索引超出数组界限”
      

  3.   

    最安全:
    if(ds!=null)
      if(ds.tables["mytbale"]!=null)
         if(ds.tables["mytbale"].DefaultView!=null)
      

  4.   

    to ajqc(失眠无神闷坐中) x= (ds.Tables["mytbale"].DefaultView.Count)!=null ? (ds.Tables["mytbale"].DefaultView.Count) : 0
    我把你上面的代码复制后,编译都不能通过 呀,提示如下!
    运算符“!=”无法应用于“int”和“<null>”类型的操作数if(ds.Tables["mytbale"].DefaultView.Count!=null)
     x=ds.Tables["mytbale"].DefaultView.Count;
    else
     x=0
    上面的if还是编译都不能通过 呀,提示如下!
    运算符“!=”无法应用于“int”和“<null>”类型的操作数
      

  5.   

    to ajqc(失眠无神闷坐中) 谢谢你的帮助,下面catch(Exception ex)中的,Exception ex在我这段代码中该怎么写呀?try
    {可能出错的过程}
    catch(Exception ex)
    {出错时的处理}
      

  6.   

    语法错误:
    没有Exception e
      

  7.   

    我修改成了下面的代码还是提示“索引超出数组界限”不是catch(Exception ex)中有没有Exception e的问题,当前代码中我用了很多个没有没有Exception e的cathc一样没有任何问题try
    {
      x=ds.Tables["mytbale"].DefaultView.Count;
    }
    catch(Exception ex)
    {
       x=0;
    }
      

  8.   

    x= (ds.Tables["mytbale"].DefaultView!=null) ? (ds.Tables["mytbale"].DefaultView.Count) : 0