Solved

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

Posted on 2011-02-22
5
509 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Inserting LocalDB Table to SQL Server C# 3 47
Removing Version of .NET in Windows 10 17 119
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 73
Error in Visual Project 10 47
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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