Reputation: 1051
I am developing a form with datagridview
.
The result I want is:
I make this on Cell_Enter Event
(I have some reason to code this on Cell_Enter
.I have to use Cell_Enter).
DataGridViewCell cell = myGrid.Rows[cursorRow].Cells[cursorCol];
myGrid.CurrentCell = cell;
myGrid.BeginEdit(true);
Clicking on Editable Cell
is OK, Clicking on ReadOnly Cell
give an Exception Error:
Error-> Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
So, Is there a solution for this problem? (When user clicks on ReadOnly Cell
, the Cursor will move to Editable
Cell.)
Edit:The solution I want is How do I move cursor to Other cell that is not Current cell?
Upvotes: 2
Views: 8776
Reputation: 740
I'm not 100% sure this will work in your situation, but I once ran into something like this due to one of our client's silly UI requirements. The quick fix was wrapping the code in BeginInvoke
. For example:
BeginInvoke((Action)delegate
{
DataGridViewCell cell = myGrid.Rows[cursorRow].Cells[cursorCol];
myGrid.CurrentCell = cell;
myGrid.BeginEdit(true);
});
Essentially this will make it execute the code after the CellEnter
event, allowing the DataGridView
to do whatever it does behind the scenes that was causing the exception.
Eventually it was refactored into a custom control that extended DataGridView
and BeginInvoke
was no longer necessary.
Upvotes: 1
Reputation: 1051
I have found a solution for this problem here.
private void myGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
//Do stuff
Application.Idle += new EventHandler(Application_Idle);
}
void Application_Idle(object sender, EventArgs e)
{
Application.Idle -= new EventHandler(Application_Idle);
myGrid.CurrentCell = myGrid[cursorCol,cursorRow];
}
Upvotes: 2
Reputation: 7082
Try to use the If.. else ..statement
if (cursorCol == 1) //When user clicks on ReadOnly Cell, the Cursor will move to Editable Cell.
{
myGrid.CurrentCell = myGrid[cursorRow, cursorCol];
}
else //When user clicks on Editable Cell, the Cursor will be on this Current Editable Cell.
{
//Do stuff
myGrid.BeginEdit(true);
}
Upvotes: 1