VB.Net deletes the last row of DataGridView

The DataGridView control had trouble deleting the last row. I searched the Internet and the solution is as follows:

1. In the design state of the form, set it to not allow users to add new rows.

2. At this time, there is no problem to delete the last line.

3. Set in the code: .AllowUserToAddRows = False, the same error occurs when deleting the last line.

The DataGridView control is added manually on the form design interface, not dynamically added using code.

It is unclear whether using code to dynamically add this control will encounter corresponding problems.

The detailed code is as follows:

Dim I As Int16
With dgv_To be invoiced
If .Rows.Count = 1 Then
  'There is only one line at this time, delete all.
  Rows.Clear ()
ElseIf .Rows.Count > 1 Then
  ' There are more than two lines at this time.
  I =. CurrentRow.Index
  'Set whether to allow rows to be added first is not allowed
  '. AllowUserToDeleteRows = False 'It can be seen from this, trying to set the code to not add new rows, but still delete the last row error.
  '.Rows (0) .Selected = True' Some netizens proposed to select any non-deleted row, and then delete the last row. After the trial, I found that there was still an error when deleting the last line.
  .Rows.RemoveAt (I)
  '.AllowUserToDeleteRows = True
  ' and then set whether to allow the addition of rows can be
End If
End With

Guess you like

Origin www.cnblogs.com/doctor-cao/p/12758155.html