c# - Deleting multiple rows in an unbound datagridview -
I have a project that reads lines from a SQL database and displays them in a Datagram view. That part works great I want to show a subset of these records by removing every non selected row in the grid. This code is:
foreach (DataGridViewRow line in dataGridView1.Rows) {if (! Row.Selected) {if (! Row.IsNewRow) {dataGridView1.Rows.Remove (line); //dataGridView1.Rows.Remove(dataGridView1.CurrentRow); }}
This removes either from the bottom to the selected area and leaves the above mentioned rows, or deletes the selected ones even with unselected rows.
Tell me in the right direction?
The rows are being changed because others are being removed, so some elements are being misplaced Are being dropped from
You can use 2 loops: 1 to reverse the selected items, and to delete the second selected items (which will not be the items previously not selected before the inverse).
foreach (DataGridView1.Rows in the DataGridViewRow row) {// stop selection in line if (! Line selected!) {If (! Row.IsNewRow) {row.Selected = true; }} And {row.Selected = false; }} // Remove the selected rows of foreach (DataGridViewRow in the dataGridView1 selected.) {DataGridView1.Rows.Remove (line); }
Comments
Post a Comment