[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-02-22
5
Medium Priority
?
517 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 1500 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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