Reputation:
How to check whether user selected row in datagrid view and this row is not newrow?
I tried so far this but not working as should:
If Not IsNothing(Grid.CurrentRow) And Not Grid.CurrentRow.IsNewRow Then
End If
First statement seems to works ok but when it comes to second it always return false even if I select NewRow
Upvotes: 3
Views: 7870
Reputation: 125197
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.
Upvotes: 3