Formating a text box after it has been databound

  • Thread starter Thread starter Mike Kearl
  • Start date Start date
M

Mike Kearl

I am databinding a winforms textbox using the following

this.txtSalesRepCollected.DataBindings.Clear();
this.txtSalesRepCollected.DataBindings.Add("Text", this.dataset.table,
"SalesRepcollected");

When it returns the value from the database it returns a double value of

120.000

How can I format this so that it is only 120.00

Somone suggested that it is quite simple and that I just extend the TextBox
and handle Format / Parse events but I dont know how to do this.

I tried the following:
this.txtSalesRepCollected.Text =
double.Parse(this.txtSalesRepCollected.Text).ToString("0.00");
This works when the value comes from the databae and formats the value
correctly but if I try to type something in it messes up the number.
 
Try testing if the Control has focus.
If it does then do not perform the formatting until event LostFocus has been
called

Hope this does you som good!

Philip
 
Take a look at then ItemDataBound event. You can access the control which
has been bound and set it's text. eg


Sub Item_Bound(sender As Object, e As DataGridItemEventArgs)

' Use the ItemDataBound event to customize the DataGrid control.
' The ItemDataBound event allows you to access the data before
' the item is displayed in the control. In this example, the
' ItemDataBound event is used to format the items in the
' CurrencyColumn in currency format.
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

' Retrieve the text of the CurrencyColumn from the DataGridItem
' and convert the value to a Double.
Dim Price As Double = Convert.ToDouble(e.Item.Cells(2).Text)

' Format the value as currency and redisplay it in the DataGrid.
e.Item.Cells(2).Text = Price.ToString("c")

End If

End Sub


--
Pete
-------
http://www.DroopyEyes.com
Audio compression components, DIB Controls, FastStrings

http://www.HowToDoThings.com
Read or write articles on just about anything
 
I am still having problems with this. I also read somewhere about adding an
additional line after the databinding.. that is something like this...

AddHandler this.txtTechUpgrades.DataBindings[0].Format but I am not sure
how to use it.. any ideas? I dont think this is everything that I need
 
Back
Top