Solved

DataGridView Progress Column works under XP but not under Vista or Windows 7...

Posted on 2011-02-22
5
503 Views
Last Modified: 2013-12-17
Hi Guys.

I have written a DataGridViewProgressColumn which works absolutely perfectly under XP.  However, under Vista and 7 it does not update and stubbornly and resolutely stays blank!

The code that paints the cell is as follows:
        protected override void Paint(Graphics g, Rectangle clipBounds,
                                      Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
                                      object value,
                                      object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
                                      DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                      DataGridViewPaintParts paintParts)
        {
            int progressVal;

            if (value != null)
            {
                progressVal = (int)value;
            }
            else
            {
                progressVal = 0;
            }

            // Draws the cell grid
            base.Paint(g, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle,
                       advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));

            const int margin = 4;

            var pb = new ProgressBar
                         {
                             Height = 15,
                             Width = cellBounds.Right - cellBounds.Left - (margin*2),
                             Value = progressVal
                         };


            var bmp = new Bitmap(pb.Width, pb.Height);

            pb.DrawToBitmap(bmp, pb.ClientRectangle);

            g.DrawImage(bmp, new Point(cellBounds.X + margin, cellBounds.Y + margin));
        }

Open in new window


Any suggestions would be appreciated
0
Comment
Question by:chrisbray
  • 4
5 Comments
 
LVL 4

Accepted Solution

by:
Veerabhadraiahhv earned 500 total points
ID: 34959492
0
 
LVL 3

Author Comment

by:chrisbray
ID: 34961178
Hi Veerabhadraiahhv:

Thanks for the suggestions.  Some of them have exactly the same problem as my implmentation, and the only one that has a hope of working is a dreadful kludge where the green bar representing the value is drawn over the top of the progress bar....

None of these explain WHY my code (and their code which is effectively the same) works under XP but NOT under 7.  If we knew WHY it doesn't work under 7 we would have a better chance of providing a proper resolution instead of a horrible kludge!

Any further suggestions?

Chris Bray
0
 
LVL 3

Author Comment

by:chrisbray
ID: 34977712
Hi Veerabhadraiahhv:

In the absence of any other input, I have worked on my own version of the horrible kludge suggested in one of your links.  It is still horrible, but having written my own version at least it works which is considerably more than you can say for the sample provided.  I would still like a proper solution, but this is better than nothing...

Important points to note:

I have kept the progress bar at a standard size.  Having a HUGE progress bar in a deep row just looks dreadful.
I have had to add a check for the OS in use.  If using XP the number of blocks displayed rounds up, so you can have part of a block sticking out of the end of the Vista style bar.  The code now checks the OS version and only draws the new bar under Vista or higher - for XP it simply displays the original progress bar which works properly under that OS.

Chris Bray
protected override void Paint(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            int progressVal;
            if (value != null)
            {
                progressVal = (int)value;
            }
            else
            {
                progressVal = 0;
            }

            // Draw the cell
            base.Paint(g, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle,
                (paintParts & ~DataGridViewPaintParts.ContentForeground));

            const int margin = 4;

            // Create a progress bar
            using (var pb = new ProgressBar
            {
                Height = 15,
                Style = ProgressBarStyle.Continuous,
                Width = cellBounds.Right - cellBounds.Left - (margin * 2),
                Value = progressVal
            })
            {
                if (pb.Width <= 0)
                {
                    pb.Width = 1;
                }

                int lHeight = pb.Height - margin;
                int lWidth = Convert.ToInt32(Math.Round(pb.Width * (progressVal / 100.0), 0 ,MidpointRounding.AwayFromZero));

                using (var bmp = new Bitmap(pb.Width, pb.Height))
                {
                    pb.DrawToBitmap(bmp, pb.ClientRectangle);
                    g.DrawImage(bmp, new Point(cellBounds.X + margin, cellBounds.Y + margin));

                    OperatingSystem osInfo = Environment.OSVersion;
                    if (osInfo.Platform != PlatformID.Win32NT || osInfo.Version.Major < 6)
                    {
                        return;
                    }

                    var barRectangle = new Rectangle(cellBounds.X + margin + 1, cellBounds.Y + 1 + (int)(margin * 1.5), lWidth - 1, lHeight);

                    if (barRectangle.Width <= 0)
                    {
                        barRectangle.Width = 1;
                    }

                    using (var myLinearGradientBrush = new LinearGradientBrush(barRectangle, Color.LightGreen,
                                                                               Color.MediumSeaGreen, LinearGradientMode.Vertical))
                    {
                        myLinearGradientBrush.SetBlendTriangularShape((float).5);
                        g.FillRectangle(myLinearGradientBrush, barRectangle);
                    }
                }
            }
        }

Open in new window

0
 
LVL 3

Author Comment

by:chrisbray
ID: 35458777
Don't know what happened here... I tried to allocate the points to Veerabhadraiahhv but the system raised a close request instead??

Can this be fixed please?
0
 
LVL 3

Author Closing Comment

by:chrisbray
ID: 35458781
The links provided were mostly inappropriate, but a solution was reached (albeit not an ideal one) by doing something similar to a suggestion in one of the postings attached to one of the links provided.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question