Solved

How can I cause the highlight to redraw when skipping cells in a custom DataGridView?

Posted on 2009-05-10
1
432 Views
Last Modified: 2013-12-17
I have created a custom DataGridView component, and one of the added features is the ability to skip readonly columns.  I have implemented this by overriding the SetCurrentCellAddressCore method.

It works perfectly, except that the cell in the readonly column is painted blue whilst the newly selected cell gets the highlight border...  

How can I cause the newly selected cell to be highlighted and the readonly cell not?
protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)

        {
 

            if (columnIndex > -1 && rowIndex > -1 && skipReadOnlyColumns && 

                this.Rows[rowIndex].Cells[columnIndex].ReadOnly && !throughMouseClick)

            {

                int newColumnIndex = columnIndex + 1;

                int newRowIndex = rowIndex;

                if (newColumnIndex == this.Columns.Count)

                {

                    newColumnIndex = 0;

                    if (newRowIndex < this.Rows.Count - 1)

                        newRowIndex++;

                }
 

                return base.SetCurrentCellAddressCore(newColumnIndex, newRowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);

            }

            else

                return base.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);

        }

Open in new window

0
Comment
Question by:chrisbray
1 Comment
 
LVL 3

Accepted Solution

by:
chrisbray earned 0 total points
ID: 24347921
The answer is to call SetSelectedCellCore:

                // Remove selection from current cell
                SetSelectedCellCore(columnIndex, rowIndex, false);
                // Change selection to new cell
                SetSelectedCellCore(newColumnIndex, newRowIndex, true);

The entire override which now supports backwards as well as forwards tabbing and mouse selection and copying of skipped cells is below in case anyone needs it.

Chris Bray

        protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)

        {
 

            if (columnIndex > -1 && rowIndex > -1 && skipReadOnlyColumns && 

                this.Rows[rowIndex].Cells[columnIndex].ReadOnly && !throughMouseClick)

            {

                int newColumnIndex;

                int newRowIndex;

                bool movingForward = this.CurrentCell.ColumnIndex < columnIndex;
 

                if (movingForward)

                {

                    newColumnIndex = columnIndex + 1;

                    newRowIndex = rowIndex;

                    if (newColumnIndex == this.Columns.Count)

                    {

                        newColumnIndex = 0;

                        if (newRowIndex < this.Rows.Count - 1)

                            newRowIndex++;

                    }

                }

                else

                { 

                    newColumnIndex = columnIndex - 1;

                    newRowIndex = rowIndex;

                    if (newColumnIndex == Columns.Count -1)

                    {

                        newColumnIndex = 0;

                        if (newRowIndex > 0)

                            newRowIndex--;

                    }

                }

                

                // Remove selection from current cell

                SetSelectedCellCore(columnIndex, rowIndex, false);

                // Change selection to new cell

                SetSelectedCellCore(newColumnIndex, newRowIndex, true);

                

                return base.SetCurrentCellAddressCore(newColumnIndex, newRowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);

            }

            else

                return base.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);

        }

Open in new window

0

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!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Round a string to two digits 12 24
Round up to 100% in .NET 10 45
Why don't I see this table in EDMX file? 2 20
COnsume rest client 6 12
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

15 Experts available now in Live!

Get 1:1 Help Now