473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Datagrid culture and money formatting issue

Still trying to clean up some datagrid formatting issues from the past.

When I bring in money values from a stored procedure, I'm getting 4 decimal
places in the grid ( which of course I only want 2).

Soooo ... I tried to create the cultureinfo object, set the format decimal
digits, then assign that to formatinfo on the column. Anyone see where I
went astray?

Dim USCultureInfo As CultureInfo = New CultureInfo("en-us")
USCultureInfo.NumberFormat.CurrencyDecimalDigits = 2

'note the use of Ken Tucker's alignment class here
Dim tbcSaleAmount As New HeaderAndDataAlignColumn
tbcSaleAmount.MappingName = "TotalSaleAmount"
tbcSaleAmount.Alignment = HorizontalAlignment.Center
tbcSaleAmount.DataAlignment = HorizontalAlignment.Right
tbcSaleAmount.HeaderText = "Sale Amount"
tbcSaleAmount.Width = 70
tbcSaleAmount.NullText = ""
tbcSaleAmount.FormatInfo = USCultureInfo
tbcSaleAmount.Format = "c"
Nov 21 '05 #1
2 3119
Hi,

In my orginal class I never thought about the format parameter.
Here is some code that will work for you. When I finish updating the sample
to work with the format and formatinfo I will update the sample on my
website.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim daEmployees As OleDbDataAdapter
Dim conn As OleDbConnection
Dim ds As New DataSet

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source =C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

daEmployees = New OleDbDataAdapter("Select * From Products", conn)
daEmployees.Fill(ds, "Products")

DataGrid1.DataSource = ds.Tables("Products")

Dim ts As New DataGridTableStyle
ts.MappingName = "Products"

ts.SelectionBackColor = Color.Yellow
ts.SelectionForeColor = Color.Red
ts.PreferredRowHeight = 25

Dim colName As New DataGridTextBoxColumn
With colName
.MappingName = "ProductName"
.HeaderText = "Name"
.Width = 300
.ReadOnly = True
End With

Dim colPrice As New HeaderAndDataAlignColumn
With colPrice
.MappingName = "UnitPrice"
.HeaderText = "Price"
.Width = 75
.Format = "c"
.Alignment = HorizontalAlignment.Center
.DataAlignment = HorizontalAlignment.Right
End With

ts.GridColumnStyles.Add(colName)
ts.GridColumnStyles.Add(colPrice)

DataGrid1.TableStyles.Add(ts)
ts = Nothing
colPrice = Nothing
colName = Nothing

DataGrid1.DataSource = ds.Tables("Products")
ds.Tables("Products").DefaultView.AllowNew = False
End Sub
The changed class

Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Decimal Then
s = CDec(Me.GetColumnValueAtRow([source],
rowNum)).ToString(Me.Format)
Else
s = Me.GetColumnValueAtRow([source], rowNum).ToString
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class
Ken
------------------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:Oq**************@TK2MSFTNGP15.phx.gbl...
Still trying to clean up some datagrid formatting issues from the past.

When I bring in money values from a stored procedure, I'm getting 4 decimal
places in the grid ( which of course I only want 2).

Soooo ... I tried to create the cultureinfo object, set the format decimal
digits, then assign that to formatinfo on the column. Anyone see where I
went astray?

Dim USCultureInfo As CultureInfo = New CultureInfo("en-us")
USCultureInfo.NumberFormat.CurrencyDecimalDigits = 2

'note the use of Ken Tucker's alignment class here
Dim tbcSaleAmount As New HeaderAndDataAlignColumn
tbcSaleAmount.MappingName = "TotalSaleAmount"
tbcSaleAmount.Alignment = HorizontalAlignment.Center
tbcSaleAmount.DataAlignment = HorizontalAlignment.Right
tbcSaleAmount.HeaderText = "Sale Amount"
tbcSaleAmount.Width = 70
tbcSaleAmount.NullText = ""
tbcSaleAmount.FormatInfo = USCultureInfo
tbcSaleAmount.Format = "c"

Nov 21 '05 #2
Thanks Ken. I did some more experimenting with that after I posted and
realized that the format parameter would not work with your class. The
tradeoff I took (in this particular form) was to simply change the sproc to
convert all my values to varchar (with a $ sign of course!). But I have
other forms that I wish to handle without changing the sprocs, so your
changes will come in handy. Thanks again!

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uX*************@TK2MSFTNGP10.phx.gbl...
Hi,

In my orginal class I never thought about the format parameter.
Here is some code that will work for you. When I finish updating the
sample
to work with the format and formatinfo I will update the sample on my
website.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim daEmployees As OleDbDataAdapter
Dim conn As OleDbConnection
Dim ds As New DataSet

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source =C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

daEmployees = New OleDbDataAdapter("Select * From Products", conn)
daEmployees.Fill(ds, "Products")

DataGrid1.DataSource = ds.Tables("Products")

Dim ts As New DataGridTableStyle
ts.MappingName = "Products"

ts.SelectionBackColor = Color.Yellow
ts.SelectionForeColor = Color.Red
ts.PreferredRowHeight = 25

Dim colName As New DataGridTextBoxColumn
With colName
.MappingName = "ProductName"
.HeaderText = "Name"
.Width = 300
.ReadOnly = True
End With

Dim colPrice As New HeaderAndDataAlignColumn
With colPrice
.MappingName = "UnitPrice"
.HeaderText = "Price"
.Width = 75
.Format = "c"
.Alignment = HorizontalAlignment.Center
.DataAlignment = HorizontalAlignment.Right
End With

ts.GridColumnStyles.Add(colName)
ts.GridColumnStyles.Add(colPrice)

DataGrid1.TableStyles.Add(ts)
ts = Nothing
colPrice = Nothing
colName = Nothing

DataGrid1.DataSource = ds.Tables("Products")
ds.Tables("Products").DefaultView.AllowNew = False
End Sub
The changed class

Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal
instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Decimal Then
s = CDec(Me.GetColumnValueAtRow([source],
rowNum)).ToString(Me.Format)
Else
s = Me.GetColumnValueAtRow([source], rowNum).ToString
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class
Ken
------------------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:Oq**************@TK2MSFTNGP15.phx.gbl...
Still trying to clean up some datagrid formatting issues from the past.

When I bring in money values from a stored procedure, I'm getting 4
decimal
places in the grid ( which of course I only want 2).

Soooo ... I tried to create the cultureinfo object, set the format decimal
digits, then assign that to formatinfo on the column. Anyone see where I
went astray?

Dim USCultureInfo As CultureInfo = New CultureInfo("en-us")
USCultureInfo.NumberFormat.CurrencyDecimalDigits = 2

'note the use of Ken Tucker's alignment class here
Dim tbcSaleAmount As New HeaderAndDataAlignColumn
tbcSaleAmount.MappingName = "TotalSaleAmount"
tbcSaleAmount.Alignment = HorizontalAlignment.Center
tbcSaleAmount.DataAlignment = HorizontalAlignment.Right
tbcSaleAmount.HeaderText = "Sale Amount"
tbcSaleAmount.Width = 70
tbcSaleAmount.NullText = ""
tbcSaleAmount.FormatInfo = USCultureInfo
tbcSaleAmount.Format = "c"

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing the code. Thank you. .. Facundo
7
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is...
1
by: RSB | last post by:
Hi Everyone, i am using the following code to transfer a DataGrid to Excel File. Every thing works ok beside the long numerice value like 0000121900000000 gets converted to 1.219E+11. how can i...
0
by: d pak | last post by:
Here is a snippit which replicates my issue. I have a datagrid which contains an input textbox on each row, binded on the serverside. However it seems that when I perform a postback to refresh teh...
4
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
3
by: Franck | last post by:
hello, i'm looking for code (C# preferably) in order to change programmatically in a datagrid the string formatting expression of one bound colum thank you
3
by: daz_oldham | last post by:
I am populating my datagrid 'by hand' by that I mean that i am not using the wizard! What I would like to know, is if I can format my colums - for example if I have a column that is returning...
5
by: Sajit | last post by:
Hi, Could anyone provide any suggestions on how to implement localization features for a multilingual website. Before you jump the guns, let me define the site structure a little more. We have a...
5
by: tshad | last post by:
I have a datagrid that I cannot get to right justify a money amount (which is just a label). No matter what I do - it still right justifies it. <asp:TemplateColumn Visible="true"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.