Question

I have a set of labels in a DataRepeater. The labels get their values from a table in a SQL database (Let say GetSQLResults gets the data from SQL database and returns a DataTable).

Dim salesDataTable As DataTable = GetSQLResults()

And for the binding

SalesLabel.DataBindings.Add("Text", salesDataTable , "Sales")

Where SalesLabel is a label in the form and "Sales" is the name of the column in the database.

What I want to do is to apply let say US money formatting to this Sales value that comes from the database. I don't know how to combine formatting information with the command that I wrote above for DataBinding. It is a Windows Form application and I am using VB .Net. Any help will be appreciated.

Was it helpful?

Solution

The Binding class has a FormatString property which you should be able to feed the standard or custom format string you desire.

I think perhaps something like:

Dim salesBinding As Binding = new Binding("Text", salesDataTable , "Sales")
salesBinding.FormatString = "C"
SalesLabel.DataBindings.Add(salesBinding)

Would do the trick.

OTHER TIPS

I tried this based on nkvu answer and a post at http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c860168f-46e5-4786-a68d-ac69c6a7a248/

Dim WithEvents salesBinding As Binding

salesBinding = New Binding("Text", salesDataTable , "Sales")
SalesLabel.DataBindings.Add(salesBinding)

Private Sub salesBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles salesBinding .Format
    If e.Value Is DBNull.Value Then
    Else
        e.Value = Format(e.Value, "c")
    End If
End Sub

This did the trick for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top