Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've rarely used a <code>DataGrid</code> and know nothing about its ability to add rows, but coming from a WPF/MVVM point of view, you don't need that anyway. Generally in WPF/MVVM, we manipulate data rather than UI controls, so the solution is easy. First we <code>Bind</code> our collection of data (in whatever shape we chose) to the <code>DataGrid.ItemsSource</code> property:</p> <pre><code>public ObservableCollection&lt;DataItem&gt; SomeDataCollection { get; set; } ... &lt;DataGrid ItemsSource="{Binding SomeDataCollection}" ... /&gt; </code></pre> <p>Now, if we want to add a new item to the bottom of the <code>DataGrid</code> we can do this in the code behind/view model:</p> <pre><code>SomeDataCollection.Add(new DataItem()); </code></pre> <p>Then if we want to add a new item to the start of the collection, we can just do this:</p> <pre><code>SomeDataCollection.Insert(0, new DataItem()); </code></pre> <p>Of course, you'll need to implement the <code>INotifyPropertyChanged</code> interface in your code behind/view model to make this work, but (hopefully) you'll be doing that anyway.</p> <hr> <p>UPDATE >>></p> <p>Sorry, I misunderstood you. I found a <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.newitemplaceholderposition.aspx" rel="nofollow noreferrer"><code>NewItemPlaceholderPosition</code> Enumeration</a> on MSDN that is used by an <a href="http://msdn.microsoft.com/en-us/library/cc491085%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>ItemCollection.IEditableCollectionView.NewItemPlaceholderPosition</code> Property</a>. Unfortunately, those linked pages don't have an code examples, so I found one in the <a href="https://stackoverflow.com/questions/2314458/in-wpf-datagrid-how-to-get-the-blank-row-on-top">in wpf datagrid how to get the blank row on top?</a> post here on StackOverflow. From the answer by @woodyiii in that post:</p> <pre><code>var view = CollectionView.GetDefaultCollectionView(EmployeeList) as IEditableCollectionView; if(view!=null) view.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning; </code></pre> <p>This does mean that you'd have to use a <code>CollectionView</code> to get this to work, but it seems like the only way... the <code>NewItemPlaceholderPosition</code> Enumeration isn't used by anything apart from the various <code>CollectionView</code> classes.</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