title: WPF datagrid pasting
body: <p>I'm having trouble pasting from a csv into the wpf datagrid - I have followed the suggestions here</p>
  <p><a href="https://docs.microsoft.com/en-us/archive/blogs/vinsibal/pasting-content-to-new-rows-on-the-wpf-datagrid" rel="nofollow noreferrer">Link</a></p>
  <p>and the code exectues with no problem - however, it seems that all the new rows are created but only the first row gets populated with data. The data seems to be constantly overwritten so that the last item that is in the clipboard data is populated in the first row and all other rows are blank.  I know this must be an index issue or something but I cannot track it down.</p>
  <p>Also when I have a look at the objects in the grid's bindable collection none of them have any data in.  Is there something in the OnPastingCellClipboardContent of the column that is going wrong (data conversion perhaps)?</p>
  <p>Any ideas (see the code below)</p>
  <pre><code>protected virtual void OnExecutedPaste(object sender, ExecutedRoutedEventArgs args)
      {
          // parse the clipboard data
          List&lt;string[]&gt; rowData = ClipboardHelper.ParseClipboardData();
          bool hasAddedNewRow = false;
  
          // call OnPastingCellClipboardContent for each cell
          int minRowIndex = Math.Max(Items.IndexOf(CurrentItem), 0);
          int maxRowIndex = Items.Count - 1;
          int minColumnDisplayIndex = (SelectionUnit != DataGridSelectionUnit.FullRow) ? Columns.IndexOf(CurrentColumn) : 0;
          int maxColumnDisplayIndex = Columns.Count - 1;
  
          int rowDataIndex = 0;
          for (int i = minRowIndex; i &lt;= maxRowIndex &amp;&amp; rowDataIndex &lt; rowData.Count; i++, rowDataIndex++)
          {
              if (CanUserAddRows &amp;&amp; i == maxRowIndex)
              {
                  // add a new row to be pasted to
                  ICollectionView cv = CollectionViewSource.GetDefaultView(Items);
                  IEditableCollectionView iecv = cv as IEditableCollectionView;
                  if (iecv != null)
                  {
                      hasAddedNewRow = true;
                      iecv.AddNew();
                      if (rowDataIndex + 1 &lt; rowData.Count)
                      {
                          // still has more items to paste, update the maxRowIndex
                          maxRowIndex = Items.Count - 1;
                      }
                  }
              }
              else if (i == maxRowIndex)
              {
                  continue;
              }
  
              int columnDataIndex = 0;
              for (int j = minColumnDisplayIndex; j &lt; maxColumnDisplayIndex &amp;&amp; columnDataIndex &lt; rowData[rowDataIndex].Length; j++, columnDataIndex++)
              {
                  DataGridColumn column = ColumnFromDisplayIndex(j);
                  column.OnPastingCellClipboardContent(Items[i], rowData[rowDataIndex][columnDataIndex]);
              }
          }
  </code></pre>
  <p>}</p>