我正在使用DataRow.IsNull方法来检查行是否为空。现在如果行为null,则需要输入默认字符串,否则返回行中的值。
然而,这似乎没有按预期工作?我错过了什么吗?
问题: 它只为某些行而不是其他行放置默认字符串。
我不确定我是否正确理解了这个过程?任何人都可以指出我正确的方向。
请注意,从数据库返回DataSet
,我为每行循环显示。
代码:
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim row = ds.Tables(0).Rows(i)
Dim obj As Foo = New Foo
With
{.FooAccNum = If(row.IsNull("accnum"), "Not Set", row.Field(Of String)("accnum")),
.FooPartsNumber = If(row.IsNull("partsnumber"), "Not Set", row.Field(Of String)("partsnumber")),
.FooGroup = If(row.IsNull("group"), "", row.Field(Of String)("group")),
.FooCustomer = If(row.IsNull("customer"), "No record exists", row.Field(Of String)
("customer"))
FooList.Add(obj )
Next
如您所见,客户列为空。
正如您在网格视图中看到的那样,它没有为客户设置一些行值"没有记录存在" 。这是为什么?
答案 0 :(得分:0)
可能是这些值不为空但只是空字符串的情况 - 然后它们在UI上显示相同但未通过IsNull
检查。额外的价值检查应该可以帮助您处理它们:
.FooCustomer = If(row.IsNull("customer") Or String.IsNullOrWhiteSpace(row.Field(Of String)("customer")),
"No record exists",
row.Field(Of String)("customer")