怎么判断这个为空?

bulls5988 2011-11-15 10:49:59
怎么判断int型变量 没有取得值?

使用DataGridView.CurrentRow.Cells[0].Value;取得选中值的时候。
cells[0]为int型。如果datagridview中没有内容就是空的时候,会返回什么值?我怎么判断控件中没有内容?
...全文
242 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
银冰冷月 2011-11-17
  • 打赏
  • 举报
回复
不应该是空 null是字符串 应该是!=“”;直接用两个双引号试试吧 我遇到过这样的问题
Bule 2011-11-17
  • 打赏
  • 举报
回复
我是来看答案的!
Bule 2011-11-17
  • 打赏
  • 举报
回复
我是来看答案的!
zhzhzhchch 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 qq4004229 的回复:]
if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[0].Value))
{
}
[/Quote]
wedding123 2011-11-16
  • 打赏
  • 举报
回复
if (DataGridView.CurrentRow.Cells[0].Value is DBNull)
xpingping 2011-11-16
  • 打赏
  • 举报
回复
dataGridView1.Rows.Count值为0 时dataGridView1不就没有内容嘛
幽水-椰子糖 2011-11-16
  • 打赏
  • 举报
回复
if(DataGridView.CurrentRow.Cells[0].Value!=Null)
{
//.....
}
qq4004229 2011-11-16
  • 打赏
  • 举报
回复
if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[0].Value))
{
}
绿领巾童鞋 2011-11-16
  • 打赏
  • 举报
回复
直接TRY就可以了,如果为空,可以在catch里面作处理
o_Sara 2011-11-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 denqh 的回复:]
C# code

if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[0].Value))
{
}
[/Quote]
+
ruanwei1987 2011-11-16
  • 打赏
  • 举报
回复
liym15 2011-11-16
  • 打赏
  • 举报
回复
if(DataGridView.CurrentRow.Cells[0].Value==string.Empty)
q3550565 2011-11-16
  • 打赏
  • 举报
回复
DataGridView.CurrentRow.Cells[0].Value as int ?? DBNull
相当于
try{retrun DataGridView.CurrentRow.Cells[0].Value as int;}
catch {retrun DBNull}

q3550565 2011-11-16
  • 打赏
  • 举报
回复
DataGridView.CurrentRow.Cells[0].Value as int ?? DBNull
相当于
try{retrun DataGridView.CurrentRow.Cells[0].Value as int;}
catch {retrun DBNull}

bulls5988 2011-11-16
  • 打赏
  • 举报
回复
问题是 IF的时候已经出错了 后面的根本执行不到了
Mr_babyzhang 2011-11-16
  • 打赏
  • 举报
回复
最好不要用try..catch
还是用这个

if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[0].Value))
{
}
bulls5988 2011-11-16
  • 打赏
  • 举报
回复
try ... catch 是对的,因为在IF之前就弹错了 谢谢大家 分少 真不好意思
stonespace 2011-11-16
  • 打赏
  • 举报
回复
其实在DataGridView和DataTable可以指定空值的类型,可以指定为DBNull,也可以指定为null,不过缺省是DBNull,
stonespace 2011-11-16
  • 打赏
  • 举报
回复
如果为空的话,绑定DataTable的DataGridView会返回一个DBNull对象,你只需要判断返回对象是否和DBNull类型就知道是否为空,

if (DataGridView.CurrentRow.Cells[0].Value is DBNull)
{//..为空值的情况
}
DENQH 2011-11-15
  • 打赏
  • 举报
回复
if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[0].Value))
{
}
加载更多回复(1)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧