Home All Groups Group Topic Archive Search About

DataGridView (.NET 2.0 Control) question about formatting

Author
28 Dec 2005 1:59 AM
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!

Author
28 Dec 2005 2:57 AM
Christopher Reed
Try using only "C".  By default, currency values should only have 2 decimal
places.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

Show quote
" g" <j> wrote in message news:9sadnbfNNosBbCzeRVn-uQ@comcast.com...
> 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!
>
Author
28 Dec 2005 6:18 AM
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.
Show quote
"Christopher Reed" <carttu@nospam.nospam> wrote in message
news:uX6MWq1CGHA.1180@TK2MSFTNGP09.phx.gbl...
> Try using only "C".  By default, currency values should only have 2
> decimal places.
> --
> Christopher A. Reed
> "The oxen are slow, but the earth is patient."
>
> " g" <j> wrote in message news:9sadnbfNNosBbCzeRVn-uQ@comcast.com...
>> 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!
>>
>
>
Author
28 Dec 2005 6:21 AM
Christopher Reed
You're right.  The format is for displaying the value, not while editing the
value.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

Show quote
" g" <j> wrote in message news:0NKdnf0sndCssy_eRVn-sg@comcast.com...
> 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.
> "Christopher Reed" <carttu@nospam.nospam> wrote in message
> news:uX6MWq1CGHA.1180@TK2MSFTNGP09.phx.gbl...
>> Try using only "C".  By default, currency values should only have 2
>> decimal places.
>> --
>> Christopher A. Reed
>> "The oxen are slow, but the earth is patient."
>>
>> " g" <j> wrote in message news:9sadnbfNNosBbCzeRVn-uQ@comcast.com...
>>> 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!
>>>
>>
>>
>
>
Author
29 Dec 2005 9:26 AM
TT (Tom Tempelaere)
Hi,

"g" wrote:

> 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,
--
Tom Tempelaere.

AddThis Social Bookmark Button