views:

1263

answers:

2

I want to get the index of the current row of my DataGridView. The problem is, that if the current row is the new row, CurrentRow is set to the last row that is not the new row. I cannot check for the rows to be selected because if a row is selected that doesn't mean it is the current row and the current row isn't necessarily selected.

I can get the index of the new row, but how can I know whether the new row is the current row?

A: 

Can this property value solve your problem?

dataGridView1.CurrentCell.RowIndex

P.K
Nope, same problem with that, I already tried it.
Ruud v A
+1  A: 

I hooked into the CellClick event via the following code:

private void uiEntries_CellClick(object sender, DataGridViewCellEventArgs e)
{
    Console.WriteLine(e.RowIndex == uiEntries.NewRowIndex);
}

When that condition is true, the user 'selected' the new row. Based upon that information, you may need to actually insert a new record (using the ..Rows.Add() method) before you can actually do anything with it since the new row doesn't actually exist in the collection; it's just there as a placeholder to represent a new row.

Michael Todd
I think this'll work. I'll try it ASAP.
Ruud v A
There is one flaw in your solution: when the user changes the current cell not by clicking but by using the keyboard, this won't work. The CurrentCellChanged event will work but this doesn't send a cell.
Ruud v A
Then you'll probably have to capture keystrokes as well and see if you should respond to the "movement." For example, if it's a "down arrow" on the next to the last line, that would indicate (perhaps?) that the user is trying to add a row. You could then perform the same steps (inserting new record, etc.) as above.
Michael Todd
Though it is a lot of hassle, I think this is the solution. Thanks very much!
Ruud v A