Format a Currency Datagrid Column

benschol

Member
Joined
Jun 8, 2005
Messages
22
Programming Experience
Beginner
Please, what is the syntax to format a Currency Datagrid Column using by code DataGridTableStyle?

For example:

oPrice.Format???? (I don't know the syntax...)

Thanks
 
Well, i'm not sure about anything connected with datagrid (i simply hate that control ... maybe you disagree with me but that's my opinion). however here is how i format string to currency say for Listview item/s.

PHP:
 Dim myCurrency as Decimal 
myCurrency = Decimal.Parse(lvitem.Subitems(2).Text)
lvitem.Subitems(2).text = String.Format("{0:n2}", myCurrency)

or

PHP:
Dim myCurrency as Decimal = Ctype(textBox1.text, Decimal)
textBox1.text = String.Format("{0:n2}", myCurrency)

or you can put all this in class and use whenever you need ...

PHP:
 Public Class myCurrency 
 
Public Sub formating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs)
 
If TypeOf sender Is TextBox Then
 
Dim txt As TextBox = DirectCast(sender, TextBox)
 
txt.Text = String.Format("{0:n2}", CType(txt.Text, Decimal))
 
End If
 
End Sub
 
Public Sub AddFormating(ByRef tb As TextBox)
 
AddHandler tb.Validating, AddressOf formating
 
End Sub
 
Public Sub RemoveFormating(ByRef tb As TextBox)
 
RemoveHandler tb.Validating, AddressOf formating
 
End Sub
 
End Class




Cheers ;)



added: note that you can add another another event within handler instead validating ... and also you can easy adapt this class for say entire listView or particular column etc. Also an usage of this class:
i.e.
PHP:
 Dim myCurrency as myCurrency = New myCurrency 
myCurrency.AddFormating(TextBox1)
 
Last edited:
Thanks Mr.Kulrom. I use this code in my project

VB.NET:
Private Sub GridColumns()
	Dim TSPaid As New DataGridTableStyle
	TSPaid.MappingName = "Payments"
	TSPaid.AllowSorting = True
	Dim oDate As New DataGridTextBoxColumn
	oDate.MappingName = "Date"
	oDate.HeaderText = "DATE"
	oDate.Width = 65
	TSPaid.GridColumnStyles.Add(oDate)
 
	.......More Columns Code..........
 
	[b]I need to apply the currency format to this column:[/b]
 
	[i]Dim oValue As New DataGridTextBoxColumn[/i]
[i]	oValue.MappingName = "Value"[/i]
[i]	oValue.HeaderText = "VALUE"[/i]
[i]	oValue.Width = 60[/i]
[i]	[b]oValue.Format........?[/b][/i]
[i]	TSPaid.GridColumnStyles.Add(oValue)[/i]
[i]	myGrid.TableStyles.Add(TSPaid)[/i]
End Sub

How I can do this?

Thanks
 
guess i wasn't much of help :(

Ok, try this:

oValue.Format("{0:n2}", Convert.ToDecimal(Container.DataItem("ColumnName")))

I'm not sure about the above code ... as i told you i really hate DG control and have never worked with it (it looks so odd) but the point is that all you need is to know how to format the string ... {0:Format} btw, you can use custom formats ##,###,00 etc.
for instance: Format Pattern N or n will return "1,200.00" General format G or g will return "1003.0515" etc.

Cheers ;)
 
The code
VB.NET:
oValue.Format("{0:n2}", Convert.ToDecimal(Container.DataItem("ColumnName")))
don´t work but the resultant idea from your two replies work very well :)

Based on this idea, the code below work fine:

VB.NET:
Dim oValue As New DataGridTextBoxColumn
Dim Res As Integer = oValue.MappingName = "Value"
Dim vFormat = String.Format("{0:n2}", Res)
oValue.MappingName = "Value"
oValue.HeaderText = "VALUE"
oValue.Width = 60
oValue.Format=vFormat
TSClientes.GridColumnStyles.Add(oValue)

Thanks again
 
Mr Kulrom:
A question by simple curiosity but, simultaneously, it gives me the opportunity to learn a little more about VB.NET. If you don't use DG Control, which is the method of your preference to display data from a DB similar to a DG Control?
 
I thought I'd answer this question too, as I tend not to use the DataGrid. Personally, I think the DataGrid is too much like a raw database. It is good for technically savvy users, but for your average "what's a mouse" kind of user, I think something a little prettier and less likely to allow incorrect input is better. With a big wall of data, like the DataGrid presents, I think that it is too easy for low aptitude users to edit the wrong data. I usually use a ListView and the user has to double-click a row to open it in a dialogue specifically designed for editing a row from that source. They then have to click the OK button to confirm their changes. These extra mouse-clicks may inhibit experienced users, so the DataGrid is better in certain situations, but I've never used one in a production app.
 
Thanks to both moderators but I have a last question: if the case is only display data (NOT EDIT DATA) like, for example, the result of searches, DG is a good and easy solution?
 
Last edited:
The DataGrid is a good and easy solution in either case. I think I can speak for kulrom as well when I say that we think it just doesn't provide as nice an interface to the average user as a ListView. The ListView option takes more work on the part of the developer, though, as it has no DataSource property so you must handle populating it yourself.
 
Paszt said:
For You Information: MSDN has a great article that shows how to create a databound listview here: Creating a Data Bound ListView Control
This article really helped me to understand databinding and it's also a very useful control.
Thanks Paszt. You are the first recipient of Rep from me for this post.
 
Back
Top