日期:2014-05-18  浏览次数:20360 次

如何判定dbnull???
我在一个Gridv显示用,绑定的数据中存在dbnull值!!
我编的程序是:
protected void grvView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowIndex >= 0)
  {
  if (!(e.Row.Cells[6].Text.ToString() == DBNull.Value.ToString()))
  {
  {
  string str = e.Row.Cells[6].Text.ToString();
  if (str != null && str.Length != 0)
  {
  int num = Convert.ToInt32(str);
  if(num>0)
  e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
  }
  }
  }
  }
  }
其中cell[6]就是存在dbnull的列....我用断点测试过,当运行到含有dbnull的行时,str就会变成“&nbsp”,但它却进了if (str != null && str.Length != 0)条件中导致出错....谁能帮我解决下啊??拜托

------解决方案--------------------
先str = str.Trim() 一下。 再 if(str != null && !str.Equals(DBNull.Value))
------解决方案--------------------
string str = e.Row.Cells[6].Text.ToString(); 
改成
string str = e.Row.Cells[6].Text;