2016-11-09 165 views
0

我是新來的使用綁定源,並且我正在尋找一種方法來修剪參數的值,然後將前導空格或尾隨空格保存到數據庫中。我正在使用存儲過程,所以我知道我可以在參數周圍包裝LTRIM(RTRIM()),但是我正在尋找一種方法在我的實際代碼中完成此操作。如何在使用綁定源時修改參數值?

我的數據綁定設置爲實際的文本框:txtNarrative

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

後來,當我通過我的節省例行我只是這樣做是爲了設置參數值:

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

是有沒有辦法直接在我的代碼中這樣做?或者我必須經過另一個步驟並在保存之前修剪txtNarrative.text?

不知道最好的辦法是在這裏, 在此先感謝。

+0

循環訪問'BindingSource'或底層的'DataTable'並在保存數據前修改文本字段中的值。如果源是正確的,那麼參數將使用正確的數據。 – jmcilhinney

回答

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 
+0

謝謝你們兩位! :d – mee123

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 

這是爲我工作!

相關問題