这个问题我不会取名字了。。
是这样的,数据库里有个字段是asc2码,我可以通过在一个文本框输入汉字,然后导入数据库时要以asc2码存入,汉字和asc2转换到是不难,问题是我想让文本框绑定那个字段(但是text属性有没法绑定),这样可以比较方便的导航(因为在同一个界面我还要查看那个字段的内容)
不知大家有没有其他思路阿
问题点数:20、回复次数:4Top
1 楼AntingZ(夕惕若)回复于 2004-09-03 23:05:57 得分 0
Binding.Parse
Binding.FormatTop
2 楼AntingZ(夕惕若)回复于 2004-09-03 23:07:21 得分 20
ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWindowsFormsBindingClassFormatTopic.htm
下面的示例创建一个 Binding,向 Parse 事件和 Format 事件添加 ConvertEventHandler 委托,并通过 DataBindings 属性向 TextBox 控件的 BindingsCollection 添加 Binding。添加到 Format 事件的 DecimalToCurrencyString 事件委托使用 ToString 方法将绑定值(Decimal 类型)格式化为货币。添加到 Parse 事件的 CurrencyStringToDecimal 事件委托将控件所显示的值回转为 Decimal 类型。
[Visual Basic]
Private Sub DecimalToCurrencyString(sender As Object, cevent As _
ConvertEventArgs)
' The method converts only to string type. Test this using the DesiredType.
If Not cevent.DesiredType Is GetType(String) Then
Exit Sub
End If
' Use the ToString method to format the value as currency ("c").
cevent.Value = CType(cevent.Value, Decimal).ToString("c")
End Sub
Private Sub CurrencyStringToDecimal(sender As Object, cevent As _
ConvertEventArgs)
' The method converts back to decimal type only.
If Not cevent.DesiredType Is GetType(Decimal) Then
Exit Sub
End If
' Converts the string back to decimal using the static ToDecimal method.
cevent.Value = Decimal.Parse(cevent.Value.ToString, _
NumberStyles.Currency, nothing)
End Sub
Private Sub BindControl
' Creates the binding first. The OrderAmount is a Decimal type.
Dim b As Binding = New Binding _
("Text", ds, "customers.custToOrders.OrderAmount")
' Add the delegates to the event
AddHandler b.Format, AddressOf DecimalToCurrencyString
AddHandler b.Parse, AddressOf CurrencyStringToDecimal
text1.DataBindings.Add(b)
End Sub
Top
3 楼zhpsam109(JACKY.昊昊)回复于 2004-09-03 23:12:05 得分 0
学习!Top
4 楼tttt0945()回复于 2004-09-05 15:00:32 得分 0
多谢夕惕若!
我想问一下,这个format和parse方法的思路是不是这样的:就拿你那个示例来说,数据库的那个字段格式是decimal型的,现在要在textbox里显示其货币型的值,这是通过format方法来实现的,如果要导回数据库,这要通过parse方法使其转变为decimal型,是这样的吗?
Top




