Solved

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

Posted on 2011-02-22
5
500 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
Comment Utility
0
 
LVL 3

Author Comment

by:chrisbray
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now