Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason it only works when you select from bottom-up is that <code>BindingSource.Current</code> points to the last item that was selected, so if you select from top-down, you're comparing with unselected items.</p> <p>You also seem to have a mistake which is causing your <code>first</code> and <code>second</code> to be reversed. You're using a postfix increment operation when you probably didn't mean to.</p> <p>Let's say <code>_bs.Position</code> is 5;</p> <pre><code>int first = _bs.Position; // first is now 5 int second = first++; // postfix increment: second is now 5, first is now 6 </code></pre> <p>As the <a href="http://msdn.microsoft.com/en-us/library/36x43w8w.aspx" rel="nofollow">documentation</a> says, </p> <blockquote> <p>the result of the operation is the value of the operand before it has been incremented</p> </blockquote> <p>So you've really done this:</p> <pre><code>second = first; first = first + 1; </code></pre> <p>It looks like you just wanted to do this:</p> <pre><code>second = first + 1; </code></pre> <p>It probably made no difference in this case but it makes for pretty confusing code. </p> <hr> <p>There are numerous approaches to what you are trying to do. Here is perhaps a more direct and understandable way:</p> <pre><code>int count = dgv.SelectedRows.Count; // DataGridView.SelectedRows is reverse order, so the first selected item is the last item in the list var firstSelected = dgv.SelectedRows[count - 1].DataBoundItem as CheckOrderLine; int firstOrderNumber = firstSelected.OrderNumber; // starting at second-last item (second selected row) for (int i = count - 2; i &gt;= 0; i--) { var row = dgv.SelectedRows[i]; var item = row.DataBoundItem as CheckOrderLine; if (item.OrderNumber == firstOrderNumber) dgv.Rows.Remove(row); } </code></pre> <p>To keep it simple I've left out updating the remaining row, and any error checking.</p> <p>Note that this will work even if the matching rows aren't consecutive. It's up to you to allow that or not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload