Formatting databound values

  • Thread starter Chad Z. Hower aka Kudzu
  • Start date
C

Chad Z. Hower aka Kudzu

I have a typed dataset that is on a WinForm and I have labels that have their
text properties bound to fields in a data table. The values are of type
decimal. When I dispaly them in the label, I want to add a currency symbol
and also insert comma's for thousands separators.

Is there anyway to specify the formatting for a label or a field without
breaking the databinding and doing it manually? Or can I specify the
databinding in codde and somehow apply formatting?


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
For that are the binding format events.
Very simple to use when you know them

But no way to hook them at design time correct? or more accurately, I was
hoping for something like the grid where you can put a format string in a
property and it uses it.

Code isnt a huge deal - I guess I'll just chalk it up to another shortcoming
of WinForms. I hope MS looks through WinForms better in the future, or
actually tries to use it themselves. Comparing it to VCL (Delphi) makes it
look really weak, and even something VB had back as far as VB3 are missing in
WinForms. :(

Thanks for the link though, will at least work better than manually filling
in the controls (well its actually more code, but a better solution).


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
For that are the binding format events.
Very simple to use when you know them

Ive said it before and I'll say it again - they WAY overdid the data
bindings to the point that they are a REAL PITA to use in many cases. If
they want to architect the snot out of things thats fine, but at LEAST
engineer in some shortcuts for the routine stuff (Re my codeproject article
on grids) or this case.. In Delphi or even VB it would be simple - put a
format string on the control, or at worst make a calculated field...
WinForms? No way.. because noone at MS actually uses the WinForms stuff, so
its "Academic".. ok rant off... At least with ADO.NET, ASP.NET and most of
the rest of .NET, people at MS actually use it for something other than
demos.. ;(

Ok this is still way too much code to do something this soddingly simple.
If you say its so simple please show me because I dont want to spend hours
on trial and error.. Here is what Ive tried:

private void DecimalToCurrencyString(object sender, ConvertEventArgs
cevent) {
if (cevent.DesiredType == typeof(string)) {
cevent.Value = ((decimal)cevent.Value).ToString("n2");
}
}

private void lablUnprocessedOrderCount_BindingContextChanged(object
sender, System.EventArgs e) {
((Control)sender).DataBindings[0].Format += new ConvertEventHandler
(DecimalToCurrencyString);
}

It hits the code - but the label now display blank instead of the value.
cevent.value is getting set to a string, and its not blank.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
I assume(d) that you known(knew) what is on this page?

I dont want to set it for a whole thread - so unless Im missing something
really obvious....

I have a typed dataset. I have a label, whose text property is databound to a
decimal in the dataset. I want to apply a specific format only to this field
- in this label. In Delphi, and VB (non WinForms) this is soddignly simple. I
have found not simple, or for that matter even relatively simple nor remotely
obvious way to do this and still keep the data binding. I would love to be
proven wrong on this - but so nearly everytime Ive encoutnered such (re,
curren row in a grid, positioning of records, and more) in Winforms, its
turned out someone got architect happy and never bothered to actually try
their creation in real practice.

Please prove me wrong or confirm my suspcions. :)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
C

Cor Ligthert

Kudzu,

Does this (old) sample from me fits you, you can make it of course generic,
because you have only to set the event handlers for a type of converting.

This is a slightly more difficult sample than your problem, because it is
about dates and it does both sides.

\\\
Private Sub myroutine()
Mybinding = New Binding("Text", ds.Tables(0), "mydatfield")
textdatfield.DataBindings.Add(Mybinding)
AddHandler mybinding.Format, AddressOf DBdateTextbox
AddHandler mybinding.Parse, AddressOf TextBoxDBdate
End sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("d")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
///

I hope this helps a little bit?

Cor
 
C

Chad Z. Hower aka Kudzu

Cor Ligthert said:
This is a slightly more difficult sample than your problem, because it
is about dates and it does both sides.

This is essentially what I did, but I didnt declare my binding at run time.
I'll move out of the event into the constructor, maybe it will work there...

Still way way too much code for something simple...


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

"What is better" question ... 6
Indy for Visual Studio Developers 3
Regenerating a Typed DS 2
Datagrid - Current row 6
VB.NET vs VB 8
Stream into String 7
Extending C# 4
DBGrid, CurrentRow. Easier way? 8

Top