如何在使用绑定源时修剪参数值?

时间:2016-11-09 01:47:17

标签: c# vb.net parameters

我是使用绑定源的新手,我在寻找一种方法来修剪参数的值,然后将前导空格或尾随空格保存到数据库中。我正在使用存储过程,所以我知道我可以在参数周围包装一个LTRIM(RTRIM()),但我正在寻找一种方法来实现我的实际代码。

我将数据绑定设置为实际文本框:txtNarrative

txtNarrative.DataBindings.Add("EditValue", bsDetails, "Narrative")

然后,当我完成保存例程时,我只是这样设置参数值:

Dim cmdInsert As SqlCommand
cmdInsert.Parameters.Add("@Narrative", SqlDbType.NVarChar, -1, "Narrative")

有没有办法直接在我的代码中执行此操作?或者我是否必须经过另一步并修改txtNarrative.text才能保存?

不确定最好的方法是什么, 提前谢谢。

2 个答案:

答案 0 :(得分:0)

根据jmcilhinney的建议,您可以循环遍历行,例如

Dim dt As DataTable = CType(bs.DataSource, DataTable)
For Each row As DataRow In dt.Rows
    row.SetField(Of String)("Narrative", row.Field(Of String)("Narrative").Trim)
Next

答案 1 :(得分:0)

For Each row As DataRow In dsFeature.Tables("tblFeatures").Rows
            If IsDBNull(row("Description")) = False Then
                row.SetField(Of String)("Description", row.Field(Of String)("Description").Trim)
            End If
        Next

这对我有用!