Link to home
Start Free TrialLog in
Avatar of chrisbray
chrisbrayFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Setting DataGridView.CurrentCell is ignored or sets a row one less than clicked on...

Hi Guys,

I have some code that selects the row clicked on.  Previously it was called only on a right click in order to ensure that the correct row was selected before the context menu was updated and displayed, but now I need to ensure that the current row changes to prevent the wrong row data being dragged in drag and drop.  Here is the code:

        private void GridMouseDown(object sender, MouseEventArgs e)
        {
            // move cursor to clicked item
            DataGridView.HitTestInfo info = grid.HitTest(e.X, e.Y);
            if (info.Type == DataGridViewHitTestType.Cell)
            {
                grid.CurrentCell = grid[0, info.RowIndex];
            }
        }

Open in new window


At the click CurrentCell is 0,0.  Testing the value of info.RowIndex prior to setting CurrentCell shows 6.  After setting CurrentCell to [0,6]  the value of CurrentCell is [4,5]!!

Can anyone suggest why, and what to do about it?

Chris Bray
Avatar of khan_webguru
khan_webguru
Flag of Australia image

Make sure if it giving the row 6 but index will be 5 because that would be 0 base indexing. Means 0 is first row and 5 would be 6 then,
Avatar of chrisbray

ASKER

Hi khan_webguru:

Thank you for your response - but unfortunately it does not seem to make sense.  If you look at the sample code, the row index is being retrieved from the HitTestInfo object which unsurprisingly returns the value zero based.

In any case, your response is spurious.  As described in the original question, when the data passed is [0,6] the current cell is set to [4,5].  I expect that when I set something to [0,6] it should result in [0,6] - not an unreasonable expectation I believe!!

Please can you revisit the question and try again?

Chris Bray
Avatar of AndyAinscow
Have you checked that your row #7 (code snippet) is actually being run ?
Hi Andy,

Thanks for the question.  However, if I had not I would not have been able to give the results outlined in the original question, would I?

In the question I said

Testing the value of info.RowIndex prior to setting CurrentCell shows 6.  After setting CurrentCell to [0,6]  the value of CurrentCell is [4,5]!!

Given that the only place where CurrentCell is set is in line 7, it should be safe to assume that it had been run!!

Chris Bray
No.  You gave no indication of how you tested the current cell - in fact it looks like it is tested elsewhere in another code block.
The most obvious explanation of why does this code not work is that it isn't even run.

Have you tested it is run, not just assumed.  (Breakpoint).  Please confirm it is being run.
Hi Andy,

At risk of repeating myself, YES!!  I had to test it to be able to give the results shown in the original question.  I even pointed out that I checked the values before and after setting the value, and reported those values.  

I am unsure how I could have made it any clearer...  however for the avoidance of doubt be advised that I manually stepped through the code block as shown, making tests on the values at each step.  The code that sets the value was indeed run, as it must have been since it is the ONLY place where the value is set and I could not have retrieved the values quoted in any other way.  After setting it to [0,6] in line seven (pressed F10 to move forward one step) I immediately tested the value of CurrentCell which returned [4,5].

I have also run additional tests to see whether hidden columns were the source of the problem (they are not, since Columns[0].Visible returns true).

However, I have now looked at the source code for the actual grid being used which t turns out inherits from DataGridView rather than being a framework control, and it seems that the base SetCurrentCellAddressCore method has been overridden to allow for hidden columns when moving using the keyboard.  I anticipate that I will find the cause in that code block...

I will keep you posted.

Chris Bray
ASKER CERTIFIED SOLUTION
Avatar of chrisbray
chrisbray
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the update and glad to hear you sorted it out.
I found the issue for myself.