SQL Server NULL值在RadGrid中转为 ,如何正确判断单元格值?
解决RadGrid单元格NULL值校验问题
方案1:直接读取数据源原始值(推荐)
不要依赖单元格渲染后的文本,直接从GridDataItem.DataItem获取绑定的原始数据,这样能拿到数据库里的NULL值,完全避开渲染产生的 。
示例代码:
If TypeOf e.Item Is GridDataItem Then Dim dataItem As GridDataItem = e.Item ' 假设绑定的是DataRowView,根据实际数据源类型调整 Dim statusValue As Object = CType(dataItem.DataItem, DataRowView)("Status") ' 判断原始值是否非空且为数值类型 If Not IsDBNull(statusValue) AndAlso IsNumeric(statusValue) Then Dim statusNum As Integer = Convert.ToInt32(statusValue) If statusNum < 90 Then ' 执行你的逻辑 End If End If End If
方案2:解码HTML文本后校验
如果必须用单元格的Text属性,可以先对文本进行HTML解码,把 转换成普通空格,再用String.IsNullOrWhiteSpace判断:
示例代码:
Imports System.Web ' ... If TypeOf e.Item Is GridDataItem Then Dim dataItem As GridDataItem = e.Item Dim myCell As TableCell = dataItem("Status") Dim decodedText As String = HttpUtility.HtmlDecode(myCell.Text) If Not String.IsNullOrWhiteSpace(decodedText) AndAlso IsNumeric(decodedText) Then Dim statusNum As Integer = Convert.ToInt32(decodedText) If statusNum < 90 Then ' 执行你的逻辑 End If End If End If
方案3:检查单元格的LiteralControl内容
RadGrid渲染空值时,通常会在单元格里添加一个包含 的LiteralControl,可以直接访问这个控件的Text:
示例代码:
If TypeOf e.Item Is GridDataItem Then Dim dataItem As GridDataItem = e.Item Dim myCell As TableCell = dataItem("Status") ' 查找LiteralControl Dim literal As LiteralControl = myCell.Controls.OfType(Of LiteralControl).FirstOrDefault() If literal IsNot Nothing Then Dim cellValue As String = literal.Text.Trim() If Not String.IsNullOrWhiteSpace(cellValue) AndAlso IsNumeric(cellValue) Then Dim statusNum As Integer = Convert.ToInt32(cellValue) If statusNum < 90 Then ' 执行你的逻辑 End If End If End If End If
内容的提问来源于stack exchange,提问作者SethD02
相关产品推荐
相关产品推荐

