1. DataGridView does not display the following new line
Usually the bottom line of the DataGridView is the newly added line of the user (line header display *). If you do not want the user to append a new row, that is, you do not want to display the new row, you can set the AllowUserToAddRows property of the DataGridView object to False.
Example:
this.dgv_PropDemo.AllowUserToAddRows = false;
However, you can append a new line to the DataGridView through the program:
this.dgv_Demo.();
Note: This can only be used in this way if DataGridView is in non-binding mode.
If the DataSource of DataGridView is bound to a DataView, you can also achieve the same effect by setting the property to False.
2. DataGridView determines that new rows are added
When the AllowUserToAddRows property of DataGridView is True, the user is allowed to append a new row, and the last row of DataGridView is the newly appended row. Use attributes to determine which row is the newly added row. In addition, the line number of the new line can be obtained by obtaining it. When there is no new line, NewRowIndex=-1.
When saving data to the database, you can judge whether it is a new row based on IsNewRow. If it is a new row, it will be saved.
3. Delete the row
1. Unconditional deletion of rows
By default, DataGridView allows users to delete rows. Select the row to be deleted and press the Delete key to delete. There is no prompt for this operation (just delete the data displayed on the interface, and the data in the database will not be deleted in reality). If the AllowUserToDeleteRows property of the DataGridView object is set to False, the user will be prohibited from deleting the row.
Example: Disable DataGridView from deleting rows
this.dgv_PropDemo.AllowUserToDeleteRows = false;
However, the row can still be deleted through the row's Remove or RemoveAt.
Example:
//Delete the first line selectedthis.dgv_PropDemo.(this.dgv_PropDemo.SelectedRows[0]);
If the DataGridView is bound to a DataView, you can also control the deletion of rows.
2. Conditional judgment when deleting rows
An event will be raised when the user deletes a row. In this event, the conditions can be judged and the deletion operation can be cancelled. (The event will be triggered only when AllowUserToDeleteRows is set to true and the delete key of the keyboard is used).
UserDeletingRow: Triggered when the row is deleted.
UserDeletedRow: The row is fired after it is deleted. Execution order: Execute the UserDeletingRow event first and then execute the UserDeletedRow event.
Example:
private void dgv_PropDemo_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { //Confirm before deletion if (("Confirm that you want to delete the selected line?", "Delete Confirm", , ) != ) { //If it is not Ok, delete = true; } } private void dgv_PropDemo_UserDeletedRow(object sender, DataGridViewRowEventArgs e) { ("This line has been deleted"); }
3. Delete the selected row
Example:
foreach (DataGridViewRow row in this.dgv_PropDemo.SelectedRows) { if (!) { this.dgv_PropDemo.(row); } }
This is what this article about DataGridView not displaying the lowest new row, judging new rows, and deleting row operations. I hope it will be helpful to everyone's learning and I hope everyone will support me more.