Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 24th, 2012, 12:51 AM
Authorized User
Points: 270, Level: 5
Points: 270, Level: 5 Points: 270, Level: 5 Points: 270, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: thrissur, kerala, India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with datagridview cell data formatting

Hi All,
I am working on a vb6 to vb.net migration project using vs 3.5. I have a datagridview.in a particular cell of this grid, user has to enter only numeric data. but after user enter data, itshould become concainated with a $ symbol (eg: if user enter 34, the cell value should become $34). And if he/she presses the hyphen ("-") in the key board, the data (ie. $34) should become -$34 and if again presses the hyphen button in key board, the data should become $34.

Any idea of how to do this.Any help appreciated.

Regards,
Sumith
__________________
sumith
 
Old May 25th, 2012, 08:29 AM
Authorized User
Points: 270, Level: 5
Points: 270, Level: 5 Points: 270, Level: 5 Points: 270, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: thrissur, kerala, India.
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it.
First, need to format the column as currency
Code:
DataGridView1.Columns("C").DefaultCellStyle.Format = "c"
Next, add eventhandler for cells
Code:
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, _       ByVal e As DataGridViewEditingControlShowingEventArgs) _       Handles DataGridView1.EditingControlShowing       Dim txtEdit As TextBox = e.Control       RemoveHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress       AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypressend sub
And, in txtedit_keypress,

Code:
Private Sub txtEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
 If e.KeyChar = "-" Then
                Dim curCell As DataGridViewCell = DataGridView1.CurrentCell
                If curCell.ColumnIndex = 2 Then
                    curCell.Value = -CDec(curCell.Value)
                    DataGridView1.RefreshEdit()
                    e.Handled = True
                End If
            End If
end sub
This will work as desired.
__________________
sumith




Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting DataGridView codehelp Visual Studio 2008 0 May 19th, 2010 01:57 AM
Accessing a cell in a DataGridView Martin Woodhouse C# 2008 aka C# 3.0 12 October 3rd, 2008 08:42 AM
datagridview cell validation kscase Pro Visual Basic 2005 1 July 12th, 2007 07:21 AM
Formatting number in DataGridView not working! RMan54 General .NET 2 July 27th, 2006 06:01 PM
Need Help - Bound DataGridView Formatting Problem mbetter .NET Framework 2.0 0 November 4th, 2005 08:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.