DataGridView (.NET 2.0 Control) question about formatting

  • Thread starter Thread starter g
  • Start date Start date
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!
 
Try using only "C". By default, currency values should only have 2 decimal
places.
 
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're right. The format is for displaying the value, not while editing the
value.
 
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,
 
Back
Top