DataGridView (.NET 2.0 Control) question about formatting

G

g

Hi,

I have what I think should be a simple question about formatting a column in
my datagridview control as currency.

I used Visual Studio 2005 to add a column to my DataGridView control named
"Price". I set the DefaultCellStyle.Format value to "C2" indicating that I
want the format of this cell to be currency with 2 decimal places. However,
when I enter a number in the column it stays as I enter it. For example, if
I entre a "1" it stays "1" rather than being formatted as "$1.00"

Doesn anyone know if I'm missing something? I've read lots of documentation
at the MSDN2 site but I can't find anything to help me. I've started to
work on custom formatting but it's seems to me that the control was designed
to allow for automatic formatting.

Thanks in Advance!
 
C

Christopher Reed

Try using only "C". By default, currency values should only have 2 decimal
places.
 
G

g

Hi Christopher,

Thanks for your response. That doesn't do the trick however.

I added a form to allow the user to add data to the DataGridView rather than
editing it directly. When i did this the correct columns were formatted as
currency. This was because I added the data using the Rows.Add() method
which can handle decimal types directly. Apparently, it's only formatted as
currency when it sees a number type and entering data directly into the
control doesn't qualify.
 
C

Christopher Reed

You're right. The format is for displaying the value, not while editing the
value.
 
G

Guest

Hi,

g said:
Hi Christopher,

Thanks for your response. That doesn't do the trick however.

I added a form to allow the user to add data to the DataGridView rather than
editing it directly. When i did this the correct columns were formatted as
currency. This was because I added the data using the Rows.Add() method
which can handle decimal types directly. Apparently, it's only formatted as
currency when it sees a number type and entering data directly into the
control doesn't qualify.

You could subscribe to the ColumnChanging event of the DataTable and set the
ProposedValue in the DataColumnChangeEventArgs. I don't know if this is the
best way, but I think it can solve your problem.

Kind regards,
 

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

Top