Avoid entering read-only column in DataGridView?

G

Guest

Column 0 in my unbound DataGridView is read-only. When the user tabs from the
last column of a row, I need the focus to go to column 1 of the next row, not
column 0.

Have tried a wide variety of events to accomplish this, but I always get:
"Operation is not valid because it results in a reentrant call to the
SetCurrentCellAddressCore function".

A simple example is:

private void dataGridItem_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridItem.CurrentCell =
dataGridItem.Rows[e.RowIndex].Cells[1];
}

Ignoring the error with Try/Catch almost works but not quite.

Would really appreciate any help.

Thanks

Bill.
 
K

Kevin Yu [MSFT]

Hi Bill,

AFAIK, there is no properties that can be set in the DataGridView to
prevent a row from being selected. Here I'm handling the CellEnter event.
HTH.

private void dataGridView1_CellEnter(object sender,
DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 0)
this.dataGridView1.CurrentCell =
this.dataGridView1.Rows[e.RowIndex].Cells[1];
}
catch
{
}
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Thanks Kevin. That almost works, in that the cell in the second column is
highlighted. However you can't type into that cell without first clicking on
the cell. That is a problem in my environment where high data volumes are
involved -- in this context users don't want to be slowed down by having to
pick up the mouse.

-- Bill.
 
K

Kevin Yu [MSFT]

Hi Bill,

As far as I can see, the performance is not slowed down by handling the
mouse events. Maybe this is the filling process that causes the performance
slowed down. In this case, you might re-consider the design of the app and
make it filling less records.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Have you found a solution to this? I am trying to do the exact same thing
and got the same error. Selecting the next cell is not an option, the next
cell needs to have the focus and be editable.
 
G

Guest

Aspnot said:
Have you found a solution to this? I am trying to do the exact same thing
and got the same error. Selecting the next cell is not an option, the next
cell needs to have the focus and be editable.

No I'm afraid I have found no solution.

Bill.
 
G

Guest

I came up with a solution. I'm not proud of it, but it works. I use the
following code in my CellEnter event.

Dim dgv As DataGridView = DirectCast(sender, DataGridView)

' Check for disabled. If so the force move to next control
If dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).ReadOnly Then
SendKeys.Send("{TAB}")
End If
 
G

Guest

Works beautifully. Thanks so much Aspnot.

Aspnot said:
I came up with a solution. I'm not proud of it, but it works. I use the
following code in my CellEnter event.

Dim dgv As DataGridView = DirectCast(sender, DataGridView)

' Check for disabled. If so the force move to next control
If dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).ReadOnly Then
SendKeys.Send("{TAB}")
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top