Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving duplicates in a List<T> based on selection from DataGridView
    primarykey
    data
    text
    <p>(Obligatory beginner alert! I can't promise I'll understand everything you tell me.)</p> <p>I have a program that uses a <code>List&lt;T&gt;</code> of a custom class <code>CheckOrderLine</code> to populate a <code>DataGridView</code> through a <code>BindingSource</code>. From within the <code>dataGrid</code> control I provide the ability to "split" records into multiple lines for imposition purposes. I'm having trouble working out the reverse of this process.</p> <p>I need to be able to make a selection in <code>dataGrid</code>, get the first selected row to start from, then iterate through each <code>CheckOrderLine</code> in the selection and compare them to the first row.</p> <p>If they all match I need to change the quantity in the first record to be the sum of all the rows selected, then remove the extra rows.</p> <p>Here's a snippet of what I have worked out on my own so far:</p> <pre><code> int index = 1; while (index &lt; rowToJoin) { _bs.RaiseListChangedEvents = false; int first = _bs.Position; int second = first++; CheckOrderLine current = (CheckOrderLine)_bs[first]; CheckOrderLine next = (CheckOrderLine)_bs[second]; if (current.OrderNumber == next.OrderNumber) { _bs.RemoveAt(second); } else { MessageBox.Show("You must select at least two matching rows to join."); return; } _bs.RaiseListChangedEvents = true; _bs.ResetBindings(false); index++; } </code></pre> <p>This works only if I select from the bottom up, and only based on the OrderNumber. In order to prevent accidents I really need to check all fields between two rows. Being able to select in any direction would be a bonus at this point.</p> <p>EDIT: I think I wasn't clear enough originally.</p> <ol> <li>I have a method that can split a row into 2-5 new rows.</li> <li>My original code can join any number of rows, but only if selected bottom to top, and based on <code>OrderNumber</code>.</li> <li>Is there a more efficient way to compare two rows other than <code>current.OrderNumber == next.OrderNumber</code>? I know I could add all my fields into the <code>if</code> statement, but i'd prefer to have it more reusable.</li> <li>Selecting in any direction is a distant second to comparing between all fields.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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