Solved

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

Posted on 2009-05-10
1
438 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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