I am trying to format a textbox to display currency. The textbox is receiving the data from a dataset and I'm not sure what I have to do now. I am also having an issue programming a save button that I added to a binding navigator. I have tried several different codes to get it work, but I guess I haven't written the right one, yet. Is there anyone who can help with this, please

Formatting/Coding
SanGupta
.net sukbir
BDev13
Imports System.Data.SqlClient
Public Class Form1
Dim bs As New BindingSource
Dim da As SqlDataAdapter
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String = _
"Server = .;Database = NorthWind;" & _
"Integrated Security = SSPI;"
Dim conn As New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from [Order Details]", conn)
da.Fill(dt)
bs.DataSource = dt
Dim b As New Binding("Text", bs, "UnitPrice")
AddHandler b.Format, AddressOf DecimalToCurrencyString
AddHandler b.Parse, AddressOf CurrencyStringToDecimal
TextBox1.DataBindings.Add(b)
BindingNavigator1.BindingSource = bs
End Sub
Private Sub DecimalToCurrencyString(ByVal sender As Object, ByVal cevent As _
ConvertEventArgs)
' The method converts only to string type. Test this using the DesiredType.
If cevent.DesiredType IsNot 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(ByVal sender As Object, ByVal cevent As _
ConvertEventArgs)
' The method converts back to decimal type only.
If cevent.DesiredType IsNot 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, Globalization.NumberStyles.Currency, Nothing)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
bs.EndEdit()
Dim cmd As New SqlCommandBuilder(da)
da.Update(dt)
End Sub
End Class
Northern Rob
At design time if you pull up the properties on the textbox and open the Advanced window you can set additional properties on the bindings one of which is the format of teh data and what to use for DbNull values.
Create a New Binding()
Set its FormatStrings and FormattingEnabled properties
Programmatically you can either add a binding with the TextBox.DataBindings.Add method
Or if you prefer you can edit a binding already in place
With TextBox.DataBindings("Text")
.FormatString = "C02" 'Currency
.FormattingEnabled = True
End With
That cause the data to be rendered in the currency format for the current culture to 2 decimal places. I think you'll find that shorter and cleaner than handling the Format and Parse events.
With regards to why your save code isn't working I couldn't tell you without seeing the code you already have in place. I should warn that if you have added the DataBase file to your project and its properties in the solution are set to Copy Always any changes to the DB will be undone everytime you restart the debugging session from within Visual Studio. This has been known to cause others to see their changes as not sticking in the past. :)
Kostadin Georgiev
Here's a walkthrough that you might find useful on how to save data to a database.
Yun