Solved

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

Posted on 2011-02-22
5
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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