Search code examples
sql-servervb.netdata-bindingstring-formattingdatarepeater

Combining Formatting and Databinding from the database


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.


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.