Use a form level variable in object Paint method

Posted on 2011-10-13
Last Modified: 2012-05-12
I have a datagridview , to which I add specialized buttons, including override the paint method.

But I now have the scenario where users want to 'tag' the cells of the dgv with color depending upon a s=user selection.

Currently I clear the dgv and reload buttons using a new object property _Highlight but that takes about 15secs.

Is there a useful way of passing the user selection across to the button class or can I override the Paint event of the DGV , to keep all the presentation code in the same class ?

/// <summary>
    /// Provides a specialised button that displays a depressed button and shows if multiple values available
    /// </summary>
    public class IssueRevButton : DataGridViewButtonCell
        private bool _IsSelected;
        private bool _MultiValues;
        private int _Highlight;

       protected override void Paint(
                Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
                int rowIndex, DataGridViewElementStates elementState, Object value, Object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts  )

            Color forgCol = cellStyle.ForeColor;
            Color backCol;
            PushButtonState pbState = PushButtonState.Normal;
            switch (_Highlight)
                case 1:
                    backCol = Color.CornflowerBlue;   //Chartreuse = light green; aliceblue>>white
                case 2:
                    backCol = Color.AliceBlue;
                case 0:
                    backCol = Color.BurlyWood;

///form display code

                        //replace default textbox cell with ButtonCell
                        GridDocs.Rows[rowid].Cells[i] = new IssueRevButton(rd.Files.Count > 1, highlightit);

Open in new window

Question by:Robberbaron (robr)
    LVL 16

    Accepted Solution

    LVL 32

    Author Closing Comment

    by:Robberbaron (robr)
    that was the key.  had to do a rewrite of my cell button object but now dont have to reload grid to change the backgrounds


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now