以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  如何让保存数据前检查行中的列不为空的代码更简洁?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=24291)

--  作者:zpx_2012
--  发布时间:2012/10/9 22:19:00
--  如何让保存数据前检查行中的列不为空的代码更简洁?

各位老师,

 

如何运用select case让下面红色的代码能够更简洁些?

 

以下是保存数据前在BeforeSaveDataRow检查行中的列不能为空的代码,行中可能有很多列的数据都要求不能为空。

 

If e.DataRow("唛头") = "" OrElse e.DataRow("业务员") = "" OrElse e.DataRow("生产单号") = "" OrElse e.DataRow.GetChildRows("生产明细").Count = 0 Then \'判断是否不符合验证要求
    MessageBox.Show("唛头,业务员,生产单号,生产明细不能为空","存盘失败",MessageBoxButtons.OK,MessageBoxIcon.Exclamation) \'提示用户
End If

 

就象下面DataColChanging的代码一样比较简洁,

\'表中的列不允许为空

\'Select Case e.DataCol.Name

\'    Case "生产单号","唛头","业务员"

\'    If e.NewValue Is Nothing Then

\'        MessageBox.Show("\'生产单号,唛头,业务员\'不允许为空!")

\'        e.Cancel = True

\'    End If

\'End Select

 

BeforeSaveDataRow又没有DataCol,NewValue这样的e参数? 

 

谢谢!


--  作者:关键下一秒
--  发布时间:2012/10/9 22:52:00
--  

Dim cols() As String={"生产单号","唛头","业务员"}
For Each col As String In cols
    If e.DataCol.Name = col Then
        If e.NewValue Is Nothing Then
            MessageBox.Show(col & "不允许为空!")
            e.Cancel = True
        End If
    End If
Next


--  作者:zpx_2012
--  发布时间:2012/10/9 23:23:00
--  

谢谢二楼,不过你用的那几个e参数都不能用,倒是你提醒我用数组,可以了。代码如下与大家分享

 

Dim ColNames As String() = {"唛头","业务员","生产单号"}
For Each ColName As String In ColNames
    If  e.DataRow(ColName) = "" Then
        MessageBox.Show("唛头,业务员,生产单号,生产明细不能为空","存盘失败",MessageBoxButtons.OK,MessageBoxIcon.Exclamation) \'提示用户
    End If
Next


--  作者:y2287958
--  发布时间:2012/10/10 0:17:00
--  
Dim ss() As String = {"唛头","业务员","生产单号"}
For i As Integer = 0 To ss.Length - 1
    If e.DataRow.IsNull(ss(i))
        MessageBox.Show("唛头,业务员,生产单号,生产明细不能为空")
        Exit For
    End If
Next