Setting CurrentCell in DGV in CellEndEdit event ignores RowIndex

S

sklett

I have a 2 column DGV. When a user enters a value in cell 0,0 I want to set
cell 0,1 as current, when they enter a value in cell 0,1 I want to set cell
1,0 current, etc, etc

basically zig-zagging down the rows from left to right.
I'm calling this method below in my CellEndEdit event:
private void AdvanceCellFocus(DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dataGridView_SerialNumberLinks.CurrentCell =
dataGridView_SerialNumberLinks[e.ColumnIndex + 1, e.RowIndex];
}
else
{
if (e.RowIndex < dataGridView_SerialNumberLinks.Rows.Count)
{
dataGridView_SerialNumberLinks.CurrentCell =
dataGridView_SerialNumberLinks[e.ColumnIndex - 1, e.RowIndex
+ 1];
}
}
}

The result is this:
- Data entered into cell 0,0
- AdvanceCellFocus() called and sets cell 0,1 as current
- when control returns to the UI, cell 1,1 is current.

So the problem appears to be that the default behavior of advancing a row
upon completing an edit operation is still happening. I'm not finding a way
to deal with it.

Anyone have any ideas? Suggestions?

Thanks for reading,
Steve
 
S

sklett

I found the solution. I needed to subclass the DGV to get to the key events
when in edit mode.


sklett said:
I found this while googling:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=327470&SiteID=1

Basically suggest suppressing the enter key event, but that will also
prevent EditMode from committing and saving the data.

I think my new, refined question is: How to prevent DGV from advancing to
next row when 'entering' out of edit mode?


sklett said:
I have a 2 column DGV. When a user enters a value in cell 0,0 I want to
set cell 0,1 as current, when they enter a value in cell 0,1 I want to set
cell 1,0 current, etc, etc

basically zig-zagging down the rows from left to right.
I'm calling this method below in my CellEndEdit event:
private void AdvanceCellFocus(DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dataGridView_SerialNumberLinks.CurrentCell =
dataGridView_SerialNumberLinks[e.ColumnIndex + 1, e.RowIndex];
}
else
{
if (e.RowIndex < dataGridView_SerialNumberLinks.Rows.Count)
{
dataGridView_SerialNumberLinks.CurrentCell =
dataGridView_SerialNumberLinks[e.ColumnIndex - 1,
e.RowIndex + 1];
}
}
}

The result is this:
- Data entered into cell 0,0
- AdvanceCellFocus() called and sets cell 0,1 as current
- when control returns to the UI, cell 1,1 is current.

So the problem appears to be that the default behavior of advancing a row
upon completing an edit operation is still happening. I'm not finding a
way to deal with it.

Anyone have any ideas? Suggestions?

Thanks for reading,
Steve
 

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