1

I'm using a datagridView, that it is bound to a bindingsource, this binding source is array of List<MyClass>. The bindingsource is constantly update from other threads(every 100 milliseconds).

* other threads can remove, add or update the bindingsource in any give time.

1) The bindingsource.Remove(object) method is not removing the object because the properties values are different, so there is no match for remove to work.

2) The bindingsource.RemoveAt(int) do remove the object, but is some case the wrong object because the bindingsource index is keep changing by other threads updates.

Can I remove an object from the bindingsource by property name? Or how to remove the right object?

The below code is the non-working RemoveAt

        dataGridView_manage_positions.SuspendLayout();
        var position = GetPosition(uniqueID);
        int index = 0;
        for (int i = 0; i < _bindingSource_manage_grid.Count; i++)
        {
            var row = (MyClass)_bindingSource_manage_grid[i];
            // EXAMPLE: the row.Net = 1.52
            if (row.ID != position.ID)
                continue;

            dataGridView_manage_positions.BeginInvoke(new Action(() =>
            {
                 // this the problem. HERE i have a race condition 
                 // EXAMPLE: the row.Net = 2.56
                 // now the Remove() will not work, because the object value is different
                _bindingSource_manage_grid.Remove(row);
            }));

            break;
        }

            dataGridView_manage_positions.ResumeLayout();

Thanks!!!!!

Sahelanthropus
  • 164
  • 1
  • 4
  • 13
  • You would be better off locating the correct instance of `MyClass` to be removed and then using `bindingsource.Remove(object)` – Martin Oct 26 '18 at 13:01
  • @Martin Parkin It doesn't work because the object values are different in the time of the remove(). and I can't get the old values. – Sahelanthropus Oct 26 '18 at 13:06
  • Hence find the correct _instance_ as I said. It would never work when passing a different object because they are not the same. – Martin Oct 26 '18 at 13:08
  • just to clearly again: The object type is the same -> the inner values are not – Sahelanthropus Oct 26 '18 at 13:09
  • This is not difficult. Enumerate the objects in the data source and then find the one that you wish to remove and remove that instance of the object. – Martin Oct 26 '18 at 13:40
  • @Martin Parkin -> even then I will not work -> from the match point to the remove point the data can change – Sahelanthropus Oct 26 '18 at 13:48
  • The datagridview is bound to a datasource containing your objects. There is no way it _cannot_ work if you remove one of the enumerated objects... – Martin Oct 26 '18 at 13:53
  • it possible i'm making the Remove() between the dataSource and the grid update(sync data) and this is why the Remove() is not working? – Sahelanthropus Oct 26 '18 at 14:03
  • @Martin Parkin I updated the above code.. if you can look again it would be great :) – Sahelanthropus Oct 26 '18 at 14:14
  • What is your condition for removing a row? – ldam Oct 26 '18 at 14:16
  • @Logan lets say I have 2 properties in my object. the first is a unique string and the second is int -> the Remove() need both properties values to match for a successful remove no? – Sahelanthropus Oct 26 '18 at 14:21
  • @Universe68 no. The object itself is already unique. If you have an object of { a, b } and then change b's value, that does not change the object itself, because the object itself *references* other objects and values. UNLESS you have overridden `.Equals()` or `.GetHashCode()` in your object to use its members to check for equality. – ldam Oct 26 '18 at 14:23
  • @Logan It's not user based action. if I understand the question right – Sahelanthropus Oct 26 '18 at 14:24
  • Ok let me ask this: is `row` a struct? – ldam Oct 26 '18 at 14:27
  • @Logon so I can override .Equals() or .GetHashCode() to check that? the Remove() will use my methods? – Sahelanthropus Oct 26 '18 at 14:27
  • @Logon on. It's Class with properties – Sahelanthropus Oct 26 '18 at 14:28
  • Have a look at https://stackoverflow.com/questions/8708632/passing-objects-by-reference-or-value-in-c-sharp – ldam Oct 26 '18 at 14:30

0 Answers0