Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help using System.Web.HttpUtility.HtmlEncode in my VB.Net application 3 54
ADO.NET ENTITY DATA MODEL 3 30
Runtime Error 2 27
Error in script 11 44
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

12 Experts available now in Live!

Get 1:1 Help Now