If you have selected new row, the DataGridView
will changes the current row on its OnValidating
method. So if you click on Button
when you have selected new row, the row remains selected, but the current row will be changed to its previous row.
So instead of checking for current row, if you want to do this check in Click
event of a Button
, it's better check for SelectedRows
:
C#
if (dataGridView1.SelectedRows.Count == 1 &&
dataGridView1.SelectedRows[0].Index == dataGridView1.NewRowIndex)
MessageBox.Show("New Row Selected");
VB.NET
If (DataGridView1.SelectedRows.Count = 1 AndAlso _
DataGridView1.SelectedRows(0).Index = DataGridView1.NewRowIndex) Then
MessageBox.Show("New Row Selected")
End If
Note: Your provided code in question, will work untill the control is focuded.