473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.N umberFormat.Cur rencyDecimalDig its = 2

'note the use of Ken Tucker's alignment class here
Dim tbcSaleAmount As New HeaderAndDataAl ignColumn
tbcSaleAmount.M appingName = "TotalSaleAmoun t"
tbcSaleAmount.A lignment = HorizontalAlign ment.Center
tbcSaleAmount.D ataAlignment = HorizontalAlign ment.Right
tbcSaleAmount.H eaderText = "Sale Amount"
tbcSaleAmount.W idth = 70
tbcSaleAmount.N ullText = ""
tbcSaleAmount.F ormatInfo = USCultureInfo
tbcSaleAmount.F ormat = "c"
Nov 21 '05 #1
2 3134
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim daEmployees As OleDbDataAdapte r
Dim conn As OleDbConnection
Dim ds As New DataSet

strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"
strConn &= "Data Source =C:\Northwind.m db;"

conn = New OleDbConnection (strConn)

daEmployees = New OleDbDataAdapte r("Select * From Products", conn)
daEmployees.Fil l(ds, "Products")

DataGrid1.DataS ource = ds.Tables("Prod ucts")

Dim ts As New DataGridTableSt yle
ts.MappingName = "Products"

ts.SelectionBac kColor = Color.Yellow
ts.SelectionFor eColor = Color.Red
ts.PreferredRow Height = 25

Dim colName As New DataGridTextBox Column
With colName
.MappingName = "ProductNam e"
.HeaderText = "Name"
.Width = 300
.ReadOnly = True
End With

Dim colPrice As New HeaderAndDataAl ignColumn
With colPrice
.MappingName = "UnitPrice"
.HeaderText = "Price"
.Width = 75
.Format = "c"
.Alignment = HorizontalAlign ment.Center
.DataAlignment = HorizontalAlign ment.Right
End With

ts.GridColumnSt yles.Add(colNam e)
ts.GridColumnSt yles.Add(colPri ce)

DataGrid1.Table Styles.Add(ts)
ts = Nothing
colPrice = Nothing
colName = Nothing

DataGrid1.DataS ource = ds.Tables("Prod ucts")
ds.Tables("Prod ucts").DefaultV iew.AllowNew = False
End Sub
The changed class

Public Class HeaderAndDataAl ignColumn
Inherits DataGridTextBox Column

Private mTxtAlign As HorizontalAlign ment = HorizontalAlign ment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(sou rce, 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.CurrencyM anager, 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.GetColumnVal ueAtRow([source], rowNum) Is Decimal Then
s = CDec(Me.GetColu mnValueAtRow([source],
rowNum)).ToStri ng(Me.Format)
Else
s = Me.GetColumnVal ueAtRow([source], rowNum).ToStrin g
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox. Font, foreBrush,
RectangleF.op_I mplicit(r), _
mDrawTxt)
End Sub

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

End Class
Ken
------------------------
"Earl" <br******@newsg roups.nospam> wrote in message
news:Oq******** ******@TK2MSFTN GP15.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.N umberFormat.Cur rencyDecimalDig its = 2

'note the use of Ken Tucker's alignment class here
Dim tbcSaleAmount As New HeaderAndDataAl ignColumn
tbcSaleAmount.M appingName = "TotalSaleAmoun t"
tbcSaleAmount.A lignment = HorizontalAlign ment.Center
tbcSaleAmount.D ataAlignment = HorizontalAlign ment.Right
tbcSaleAmount.H eaderText = "Sale Amount"
tbcSaleAmount.W idth = 70
tbcSaleAmount.N ullText = ""
tbcSaleAmount.F ormatInfo = USCultureInfo
tbcSaleAmount.F ormat = "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***@bellsout h.net> wrote in message
news:uX******** *****@TK2MSFTNG P10.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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim daEmployees As OleDbDataAdapte r
Dim conn As OleDbConnection
Dim ds As New DataSet

strConn = "Provider = Microsoft.Jet.O LEDB.4.0;"
strConn &= "Data Source =C:\Northwind.m db;"

conn = New OleDbConnection (strConn)

daEmployees = New OleDbDataAdapte r("Select * From Products", conn)
daEmployees.Fil l(ds, "Products")

DataGrid1.DataS ource = ds.Tables("Prod ucts")

Dim ts As New DataGridTableSt yle
ts.MappingName = "Products"

ts.SelectionBac kColor = Color.Yellow
ts.SelectionFor eColor = Color.Red
ts.PreferredRow Height = 25

Dim colName As New DataGridTextBox Column
With colName
.MappingName = "ProductNam e"
.HeaderText = "Name"
.Width = 300
.ReadOnly = True
End With

Dim colPrice As New HeaderAndDataAl ignColumn
With colPrice
.MappingName = "UnitPrice"
.HeaderText = "Price"
.Width = 75
.Format = "c"
.Alignment = HorizontalAlign ment.Center
.DataAlignment = HorizontalAlign ment.Right
End With

ts.GridColumnSt yles.Add(colNam e)
ts.GridColumnSt yles.Add(colPri ce)

DataGrid1.Table Styles.Add(ts)
ts = Nothing
colPrice = Nothing
colName = Nothing

DataGrid1.DataS ource = ds.Tables("Prod ucts")
ds.Tables("Prod ucts").DefaultV iew.AllowNew = False
End Sub
The changed class

Public Class HeaderAndDataAl ignColumn
Inherits DataGridTextBox Column

Private mTxtAlign As HorizontalAlign ment = HorizontalAlign ment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal
instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(sou rce, 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.CurrencyM anager, 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.GetColumnVal ueAtRow([source], rowNum) Is Decimal Then
s = CDec(Me.GetColu mnValueAtRow([source],
rowNum)).ToStri ng(Me.Format)
Else
s = Me.GetColumnVal ueAtRow([source], rowNum).ToStrin g
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox. Font, foreBrush,
RectangleF.op_I mplicit(r), _
mDrawTxt)
End Sub

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

End Class
Ken
------------------------
"Earl" <br******@newsg roups.nospam> wrote in message
news:Oq******** ******@TK2MSFTN GP15.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.N umberFormat.Cur rencyDecimalDig its = 2

'note the use of Ken Tucker's alignment class here
Dim tbcSaleAmount As New HeaderAndDataAl ignColumn
tbcSaleAmount.M appingName = "TotalSaleAmoun t"
tbcSaleAmount.A lignment = HorizontalAlign ment.Center
tbcSaleAmount.D ataAlignment = HorizontalAlign ment.Right
tbcSaleAmount.H eaderText = "Sale Amount"
tbcSaleAmount.W idth = 70
tbcSaleAmount.N ullText = ""
tbcSaleAmount.F ormatInfo = USCultureInfo
tbcSaleAmount.F ormat = "c"

Nov 21 '05 #3

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

Similar topics

24
3530
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
4674
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 quite ugly. The black area is larger then it is once an item is added. How can I solve this problem either by: A) Having only the header black and the rest of the empty block white or gray (preffered solution) B) Having the color gray or white...
1
1474
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 disable that conversion? Thanks for the Help RSB
0
1315
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 numbers, they are not changing due to the foreach loop I have in this code. When I comment out the foreach loop, the numbers refresh fine (but the text boxs ovbiously no longer get created). I have placed this code in the OnItemDataBind method,...
4
3236
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
1661
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
2055
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 100.0500, how would I get it to display £100.05. Also, is it possible to create "total rows" to total up the values of a given column?
5
3596
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 global site and around 40 country sites. Each of these country sites have localised information both in the native language (fr,de,de-at,it,ar,ar-ae etc). Also, each of these local country sites would have an english counterpart (fr-en,de-en etc)....
5
2704
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" itemStyle-Width="100px" HeaderText="Amount Per Job" HeaderStyle-Font-Bold="true" ItemStyle-VerticalAlign="middle" ItemStyle-HorizontalAlign="right" > <itemtemplate> <asp:Label ID="JobBoardPriceDisplay" style="text-align:right" runat="server"/> </itemtemplate>
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.