Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2006

Tip: Looking for answers? Try searching our database.

How to properly code GetFormattedValue in custom GridViewCell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James H. Hansen - 23 Dec 2006 19:49 GMT
I found an excellent article in the MSDN library by Règis Brid showing how
to create a custom DataGridView column for NumericUpDown controls at
http://msdn2.microsoft.com/en-us/library/aa730881(vs.80).aspx I am using the
article as a basis for a numeric textbox column, but I am really puzzling
over some of the code in the DataGridViewNumericUpDownCell class inherited
from DataGridViewTextBoxCell.

The GetFormattedValue method looks like this:

protected override object
        GetFormattedValue(object value,
        int rowIndex,
        ref DataGridViewCellStyle cellStyle,
        TypeConverter valueTypeConverter,
        TypeConverter formattedValueTypeConverter,
        DataGridViewDataErrorContexts context)
{
   // By default, the base implementation converts the Decimal 1234.5 into
   // the string "1234.5"
   object formattedValue
   = base.GetFormattedValue(value, rowIndex,
                            ref cellStyle,
                            valueTypeConverter,
                            formattedValueTypeConverter,
                            context);
   string formattedNumber = formattedValue as string;
   if (!string.IsNullOrEmpty(formattedNumber) && value != null)
   {
       Decimal unformattedDecimal = System.Convert.ToDecimal(value);
       Decimal formattedDecimal =
System.Convert.ToDecimal(formattedNumber);
       if (unformattedDecimal == formattedDecimal)
       {
           // The base implementation of GetFormattedValue (which triggers
the
           // CellFormatting event) did nothing else than the typical
1234.5
           // to "1234.5" conversion. But depending on the values of
           // ThousandsSeparator and DecimalPlaces, this may not be the
actual
           //string displayed. The real formatted value may be "1,234.500"
           return formattedDecimal.ToString(
               (this.ThousandsSeparator ? "N" : "F")
               + this.DecimalPlaces.ToString()
               );
       }
   }
   return formattedValue;
}

(I've slightly reformatted the above text to avoid superwide lines, but
haven't changed the code.)

Two things bother me:

First, the call to base.GetFormattedValue followed by the check for null or
empty string. According to the docs for the framework, the method returns
null "if the cell does not belong to a DataGridView control". If that is the
case, it seems there should be an easier way to test for that condition than
formatting the value. As for the empty string, I'm not sure what that is
about, but I suspect it has to do with null values.

Second, inside the "if" condition following the call to the base method,
there are two Convert.ToDecimal calls and a comparison of the results. I am
at pretty much a total loss to understand all that! I might guess that there
is an assumption that if they agree somebody else is doing some formatting
and we don't want to muck it up, but this seems like a thin hypothesis.

No other custom DataGridView column code I've looked at does anything like
this, although a few go out of their way to define sensible default values
in various cases.

I would like to avoid what appears to be a lot of overhead here because from
what I've read GetFormattedValue gets called a lot to check sizes of columns
and such as well as to format display values.  In any case, I would like to
understand the example

Anybody have any insight on these two bits of coding?

...Jim
James H. Hansen - 29 Dec 2006 15:37 GMT
Am I to understand from the lack of response that nobody has any idea how this
code works?

>I found an excellent article in the MSDN library by Règis Brid showing how to
>create a custom DataGridView column for NumericUpDown controls at
[quoted text clipped - 71 lines]
>
> ...Jim

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.