Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use the collection that's bound - not the 'Items' property on the grid. ItemsSource will point to your collection that is bound:</p> <pre><code>SomeGrid.ItemsSource = SomeCollection; SomeCollection.Add(new ItemOfTheRightType()); </code></pre> <p>or</p> <pre><code>(SomeGrid.ItemsSource as SomeCollection).Add(new ItemOfTheRightType()); </code></pre> <p>The error says that you can't use Grid.Items if you are binding using Grid.ItemsSource</p> <p>Edit:</p> <p>If you don't know the item type at runtime (maybe because this is a 3rd party using the control etc and you want a generic add method) you need to call the .Add method on the underlying interface. Most list types inherit from IList in the .NET framework</p> <p>I'm no VB expert, I much prefer c# so I'll give you the c#. You need to check for the underlying type first:</p> <p>in c#</p> <pre><code>if(grid.ItemsSource is IList) { (grid.ItemsSource as IList).Add(new childType()); &lt;-- other issue here.. } </code></pre> <p>The problem you have though is that if you are adding a new item to the collection and you don't know the list type, IList requires an instance of the object to add to the list</p> <ul> <li>solution is to use reflection:</li> </ul> <p><a href="https://stackoverflow.com/questions/511315/dynamically-creating-a-new-instance-of-ilists-type">Dynamically creating a new instance of IList&#39;s type</a></p> <p>An interesting late answer is:</p> <pre><code>var collectionType = targetList.GetType().GetProperty("Item").PropertyType; var constructor = collectionType.GetConstructor(Type.EmptyTypes); var newInstance = constructor.Invoke(null); </code></pre> <p>Which might work</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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