Known value: datagridviewrow DataGridViewRow
Condition: DataGridView bound to a DataTable
Request: It corresponds to the DataRow
Solution: DataRow datarow = (Datagridviewrow.databounditem as DataRowView). Row;
Need to explain:
(1) The Datagridview.currentrow property exposes the current line of DataGridView, which is the row that contains the cell that the user clicked. This property is a DataGridViewRow object.
(2) the DataGridViewRow object obtained in this way contains a Databounditem attribute that exposes the underlying data bound to DataGridView and is responsible for the data displayed by the current row. This property is of type object because there is no restriction that DataGridView must be bound to the database. However, when bound to a dataset, the property is actually a DataGridView object, so it can be cast to this type.
Find the row you want to delete, such as to delete the selected row. The key is the Databounditem property of the DataGridViewRow. Through which he can get the corresponding lines in the DataTable.
DataRowView DRV = xxxdgv.selectedrows[0]. Databounditem as DataRowView;
Drv. Row is the row of the DataTable that you bind to DataGridView. You can do anything with him.
If you want to remove this line from the DataTable, you can DRV. Row.delete ();
or DRV. Row.Table.Rows.Remove (DRV. ROW); So the corresponding line in the DataGridView is deleted.
When a DataRow object has just been created and its state is detached, it is an isolated existence, so after the DataRow has been built, the cells in the DataRow are populated with data and passed DATATABLE.ROWS.ADD (DataRow) The Datatable,datarow method adds this DataRow to the DataTable and the status of the DataRow is changed to added.
When this DataRow has been modified, the DataRow state is converted to modified, When the DataRow is deleted using the Datarow.delete () method, the DataRow state will be converted to deleted, but this line still exists in the DataTable, but the state is changed, when the number of rows is viewed with DataTable.Rows.Count, the same as before the deletion.
This DataRow is removed from the DataTable only after the Datatable.remove (DataRow) method is called, and the state is returned to the detached orphan state. However, the data set Datatable.remove (DataRow) method for remote remoting cannot be removed from the meter; only the Datarow.delete () method and the AcceptChanges () method can be used
MSDN explains the Datarow.delete method: If the line's RowState is "Added", the row will be removed from the table. After using the Delete method, RowState becomes "Deleted". It remains "deleted" until you call AcceptChanges. You can cancel the deletion of a row by calling RejectChanges.
With delete (), the HasChanges () =true of the dataset;
With remove (), the HasChanges () =false of the dataset;
The Databounditem property of DataGridViewRow