Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridView Cell Value trouble.

$
0
0

Heya People,

I've got a little problem that i don't know how to solve...

I am using a custom datagridview cell which only accepts decimal input for currency and formats it as such.

When the program is running and i enter the cell and edit the content all is fine.

When i leave the cell using the TAB key there is no problem, the entered value is formatted and when inspecting the .Value property of this control it shows me the correct value. (e.g. $ 1500,00 in the cell gives 1500.00 as output)

However when i leave the cell and click on another cell (or any other control on the form for that matter), i get an exception and when inspecting the .Value property it gives me the formatted value instead of the normal value. (e.g. $ 1500,00 will return me $ 1500,00)

Do i need to override another method or event to prevent this behaviour?

The following method was overridden by me and returns the correct result when using TAB to leave the cell, but returns the incorrect result when the cell is left using the mouseclick.

protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
    if (this.GetValue(rowIndex) == null)
        return base.GetFormattedValue(
            string.Empty,
            rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context
        );
    else
        return base.GetFormattedValue(
            string.Format("{0} {1}", this.currencysymbol, this.GetValue(rowIndex)),
            rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context
        );
}

Can someone please point me in the right correction?

Thanks in advance!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>