WPF datagrid pasting
Asked Answered
V

6

16

I'm having trouble pasting from a csv into the wpf datagrid - I have followed the suggestions here

Link

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.

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)?

Any ideas (see the code below)

protected virtual void OnExecutedPaste(object sender, ExecutedRoutedEventArgs args)
    {
        // parse the clipboard data
        List<string[]> 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 <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
        {
            if (CanUserAddRows && 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 < 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 < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
            {
                DataGridColumn column = ColumnFromDisplayIndex(j);
                column.OnPastingCellClipboardContent(Items[i], rowData[rowDataIndex][columnDataIndex]);
            }
        }

}

Vincevincelette answered 7/11, 2010 at 16:38 Comment(1)
The post from Vincent has been move to: learn.microsoft.com/en-us/archive/blogs/vinsibal/… It is referred to all over the place, but it took me quite awhile to find it. Microsoft does not have very good indexing on their blogs. I found it from someone named Bob on this Page: social.msdn.microsoft.com/Forums/aspnet/en-US/… Thanks Bob!! Maybe Microsoft should update the article?Concepcion
V
5

For those interested - there does seem to be something going wrong with the columns attempt to update the value of the bindable object - possible data type conversion so i have implemented this myself and it works like a charm now.

protected virtual void OnExecutedPaste(object sender, ExecutedRoutedEventArgs args)
    {
        // parse the clipboard data
        List<string[]> 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 <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
        {
            if (CanUserAddRows && 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 < 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 < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
            {
                DataGridColumn column = ColumnFromDisplayIndex(j);
                string propertyName = ((column as DataGridBoundColumn).Binding as Binding).Path.Path;
                object item = Items[i];
                object value = rowData[rowDataIndex][columnDataIndex];
                PropertyInfo pi = item.GetType().GetProperty(propertyName);
                if (pi != null)
                {
                   object convertedValue = Convert.ChangeType(value, pi.PropertyType);
                    item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null);                    
                }
                //column.OnPastingCellClipboardContent(item, rowData[rowDataIndex][columnDataIndex]);
            }
        }

}

Vincevincelette answered 7/11, 2010 at 17:7 Comment(0)
A
21

Thanks,

The post is very good by Vincent but KlausG Added some corrections in it that should also be considered to work with FrameWork 4.0. Very important

Original Web Site post from Vincent: https://learn.microsoft.com/en-us/archive/blogs/vinsibal/pasting-content-to-new-rows-on-the-wpf-datagrid

Note: If you can't add row ensure that you have a default constructor for your item.

Updated 2018-03-06 (to support DataGrid with columns re-ordered by user)

Updated 2020-12-03 (according to Cesar Morigaki solution-correction and message from Antonio Rodríguez) Thanks to both!!!

Usage

<myControl:CustomDataGrid ...

CustomDataGrid

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HQ.Wpf.Util.MyControl
{
    public class CustomDataGrid : DataGrid
    {
        public event ExecutedRoutedEventHandler ExecutePasteEvent;
        public event CanExecuteRoutedEventHandler CanExecutePasteEvent;

        // ******************************************************************
        static CustomDataGrid()
        {
            CommandManager.RegisterClassCommandBinding(
                typeof(CustomDataGrid),
                new CommandBinding(ApplicationCommands.Paste,
                    new ExecutedRoutedEventHandler(OnExecutedPasteInternal),
                    new CanExecuteRoutedEventHandler(OnCanExecutePasteInternal)));
        }

        // ******************************************************************
        #region Clipboard Paste

        // ******************************************************************
        private static void OnCanExecutePasteInternal(object target, CanExecuteRoutedEventArgs args)
        {
            ((CustomDataGrid)target).OnCanExecutePaste(target, args);
        }

        // ******************************************************************
        /// <summary>
        /// This virtual method is called when ApplicationCommands.Paste command query its state.
        /// </summary>
        /// <param name="args"></param>
        protected virtual void OnCanExecutePaste(object target, CanExecuteRoutedEventArgs args)
        {
            if (CanExecutePasteEvent != null)
            {
                CanExecutePasteEvent(target, args);
                if (args.Handled)
                {
                    return;
                }
            }

            args.CanExecute = CurrentCell != null;
            args.Handled = true;
        }

        // ******************************************************************
        private static void OnExecutedPasteInternal(object target, ExecutedRoutedEventArgs args)
        {
            ((CustomDataGrid)target).OnExecutedPaste(target, args);
        }

        // ******************************************************************
        /// <summary>
        /// This virtual method is called when ApplicationCommands.Paste command is executed.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="args"></param>
        protected virtual void OnExecutedPaste(object target, ExecutedRoutedEventArgs args)
        {
            if (ExecutePasteEvent != null)
            {
                ExecutePasteEvent(target, args);
                if (args.Handled)
                {
                    return;
                }
            }

            // parse the clipboard data            [row][column]
            List<string[]> clipboardData = HQ.Util.General.Clipboard.ClipboardHelper2.ParseClipboardData();

            bool hasAddedNewRow = false;

            Debug.Print(">>> DataGrid Paste: >>>");
#if DEBUG
            StringBuilder sb = new StringBuilder();
#endif 
            int minRowIndex = Items.IndexOf(CurrentItem);
            int maxRowIndex = Items.Count - 1;
            int startIndexOfDisplayCol = (SelectionUnit != DataGridSelectionUnit.FullRow) ? CurrentColumn.DisplayIndex : 0;
            int clipboardRowIndex = 0;
            for (int i = minRowIndex; i <= maxRowIndex && clipboardRowIndex < clipboardData.Count; i++, clipboardRowIndex++)
            {
                if (i < this.Items.Count)
                {
                    CurrentItem = Items[i];

                    BeginEditCommand.Execute(null, this);

                    int clipboardColumnIndex = 0;
                    for (int j = startIndexOfDisplayCol; clipboardColumnIndex < clipboardData[clipboardRowIndex].Length; j++, clipboardColumnIndex++)
                    {
                        // DataGridColumn column = ColumnFromDisplayIndex(j);
                        DataGridColumn column = null;
                        foreach (DataGridColumn columnIter in this.Columns)
                        {
                            if (columnIter.DisplayIndex == j)
                            {
                                column = columnIter;
                                break;
                            }
                        }

                        column?.OnPastingCellClipboardContent(Items[i], clipboardData[clipboardRowIndex][clipboardColumnIndex]);

#if DEBUG
                        sb.AppendFormat("{0,-10}", clipboardData[clipboardRowIndex][clipboardColumnIndex]);
                        sb.Append(" - ");
#endif
                    }

                    CommitEditCommand.Execute(this, this);
                    if (i == maxRowIndex)
                    {
                        maxRowIndex++;
                        hasAddedNewRow = true;
                    }
                }

                Debug.Print(sb.ToString());
#if DEBUG
                sb.Clear();
#endif
            }

            // update selection
            if (hasAddedNewRow)
            {
                UnselectAll();
                UnselectAllCells();

                CurrentItem = Items[minRowIndex];

                if (SelectionUnit == DataGridSelectionUnit.FullRow)
                {
                    SelectedItem = Items[minRowIndex];
                }
                else if (SelectionUnit == DataGridSelectionUnit.CellOrRowHeader ||
                         SelectionUnit == DataGridSelectionUnit.Cell)
                {
                    SelectedCells.Add(new DataGridCellInfo(Items[minRowIndex], Columns[startIndexOfDisplayCol]));

                }
            }
        }

        // ******************************************************************
        /// <summary>
        ///     Whether the end-user can add new rows to the ItemsSource.
        /// </summary>
        public bool CanUserPasteToNewRows
        {
            get { return (bool)GetValue(CanUserPasteToNewRowsProperty); }
            set { SetValue(CanUserPasteToNewRowsProperty, value); }
        }

        // ******************************************************************
        /// <summary>
        ///     DependencyProperty for CanUserAddRows.
        /// </summary>
        public static readonly DependencyProperty CanUserPasteToNewRowsProperty =
            DependencyProperty.Register("CanUserPasteToNewRows",
                                        typeof(bool), typeof(CustomDataGrid),
                                        new FrameworkPropertyMetadata(true, null, null));

        // ******************************************************************
        #endregion Clipboard Paste

        private void SetGridToSupportManyEditEitherWhenValidationErrorExists()
        {
            this.Items.CurrentChanged += Items_CurrentChanged;


            //Type DatagridType = this.GetType().BaseType;
            //PropertyInfo HasCellValidationProperty = DatagridType.GetProperty("HasCellValidationError", BindingFlags.NonPublic | BindingFlags.Instance);
            //HasCellValidationProperty.
        }

        void Items_CurrentChanged(object sender, EventArgs e)
        {
            //this.Items[0].
            //throw new NotImplementedException();
        }

        // ******************************************************************
        private void SetGridWritable()
        {
            Type DatagridType = this.GetType().BaseType;
            PropertyInfo HasCellValidationProperty = DatagridType.GetProperty("HasCellValidationError", BindingFlags.NonPublic | BindingFlags.Instance);
            if (HasCellValidationProperty != null)
            {
                HasCellValidationProperty.SetValue(this, false, null);
            }
        }

        // ******************************************************************
        public void SetGridWritableEx()
        {
            BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
            PropertyInfo cellErrorInfo = this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags);
            PropertyInfo rowErrorInfo = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags);
            cellErrorInfo.SetValue(this, false, null);
            rowErrorInfo.SetValue(this, false, null);
        }

        // ******************************************************************
    }
}

Also: Clipboard Helper:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows;
using System.IO;

namespace HQ.Util.General.Clipboard
{
    public static class ClipboardHelper
    {
        public delegate string[] ParseFormat(string value);

        public static List<string[]> ParseClipboardData()
        {
            List<string[]> clipboardData = null;
            object clipboardRawData = null;
            ParseFormat parseFormat = null;

            // get the data and set the parsing method based on the format
            // currently works with CSV and Text DataFormats            
            IDataObject dataObj = System.Windows.Clipboard.GetDataObject();
            if ((clipboardRawData = dataObj.GetData(DataFormats.CommaSeparatedValue)) != null)
            {
                parseFormat = ParseCsvFormat;
            }
            else if((clipboardRawData = dataObj.GetData(DataFormats.Text)) != null)
            {
                parseFormat = ParseTextFormat;
            }

            if (parseFormat != null)
            {
                string rawDataStr = clipboardRawData as string;
                
                if (rawDataStr == null && clipboardRawData is MemoryStream)
                {
                    // cannot convert to a string so try a MemoryStream
                    MemoryStream ms = clipboardRawData as MemoryStream;
                    StreamReader sr = new StreamReader(ms);
                    rawDataStr = sr.ReadToEnd();
                }
                Debug.Assert(rawDataStr != null, string.Format("clipboardRawData: {0}, could not be converted to a string or memorystream.", clipboardRawData));

                string[] rows = rawDataStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                if (rows != null && rows.Length > 0)
                {
                    clipboardData = new List<string[]>();
                    foreach (string row in rows)
                    {
                        clipboardData.Add(parseFormat(row));
                    }
                }
                else
                {
                    Debug.WriteLine("unable to parse row data.  possibly null or contains zero rows.");
                }
            }

            return clipboardData;
        }

        public static string[] ParseCsvFormat(string value)
        {
            return ParseCsvOrTextFormat(value, true);
        }

        public static string[] ParseTextFormat(string value)
        {
            return ParseCsvOrTextFormat(value, false);
        }

        private static string[] ParseCsvOrTextFormat(string value, bool isCSV)
        {
            List<string> outputList = new List<string>();

            char separator = isCSV ? ',' : '\t';
            int startIndex = 0;
            int endIndex = 0;

            for (int i = 0; i < value.Length; i++)
            {
                char ch = value[i];
                if (ch == separator)
                {
                    outputList.Add(value.Substring(startIndex, endIndex - startIndex));

                    startIndex = endIndex + 1;
                    endIndex = startIndex;
                }
                else if (ch == '\"' && isCSV)
                {
                    // skip until the ending quotes
                    i++;
                    if (i >= value.Length)
                    {
                        throw new FormatException(string.Format("value: {0} had a format exception", value));
                    }
                    char tempCh = value[i];
                    while (tempCh != '\"' && i < value.Length)
                        i++;

                    endIndex = i;
                }
                else if (i + 1 == value.Length)
                {
                    // add the last value
                    outputList.Add(value.Substring(startIndex));
                    break;
                }
                else
                {
                    endIndex++;
                }
            }

            return outputList.ToArray();
        }
    }
}

CsvHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using MoreLinq; // https://mcmap.net/q/325983/-how-to-find-item-with-max-value-using-linq-duplicate

namespace HQ.Util.General.CSV
{
    public class CsvHelper
    {
        public static Dictionary<LineSeparator, Func<string, string[]>>  DictionaryOfLineSeparatorAndItsFunc = new Dictionary<LineSeparator, Func<string, string[]>>();

        static CsvHelper()
        {
            DictionaryOfLineSeparatorAndItsFunc[LineSeparator.Unknown] = ParseLineNotSeparated;
            DictionaryOfLineSeparatorAndItsFunc[LineSeparator.Tab] = ParseLineTabSeparated;
            DictionaryOfLineSeparatorAndItsFunc[LineSeparator.Semicolon] = ParseLineSemicolonSeparated;
            DictionaryOfLineSeparatorAndItsFunc[LineSeparator.Comma] = ParseLineCommaSeparated;
        }

        // ******************************************************************
        public enum LineSeparator
        {
            Unknown = 0,
            Tab,
            Semicolon,
            Comma
        }

        // ******************************************************************
        public static LineSeparator GuessCsvSeparator(string oneLine)
        {
            List<Tuple<LineSeparator, int>> listOfLineSeparatorAndThereFirstLineSeparatedValueCount = new List<Tuple<LineSeparator, int>>();

            listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple<LineSeparator, int>(LineSeparator.Tab, CsvHelper.ParseLineTabSeparated(oneLine).Count()));
            listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple<LineSeparator, int>(LineSeparator.Semicolon, CsvHelper.ParseLineSemicolonSeparated(oneLine).Count()));
            listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple<LineSeparator, int>(LineSeparator.Comma, CsvHelper.ParseLineCommaSeparated(oneLine).Count()));

            Tuple<LineSeparator, int> bestBet = listOfLineSeparatorAndThereFirstLineSeparatedValueCount.MaxBy((n)=>n.Item2);

            if (bestBet != null && bestBet.Item2 > 1)
            {
                return bestBet.Item1;
            }

            return LineSeparator.Unknown;
        }
        
        // ******************************************************************
        public static string[] ParseLineCommaSeparated(string line)
        {
            // CSV line parsing : From "jgr4" in http://www.kimgentes.com/worshiptech-web-tools-page/2008/10/14/regex-pattern-for-parsing-csv-files-with-embedded-commas-dou.html
            var matches = Regex.Matches(line, @"\s?((?<x>(?=[,]+))|""(?<x>([^""]|"""")+)""|""(?<x>)""|(?<x>[^,]+)),?",
                                        RegexOptions.ExplicitCapture);

            string[] values = (from Match m in matches
                               select m.Groups["x"].Value.Trim().Replace("\"\"", "\"")).ToArray();

            return values;
        }

        // ******************************************************************
    public static string[] ParseLineTabSeparated(string line)
    {
        //var matchesTab = Regex.Matches(line, @"\s?((?<x>(?=[\t]+))|""(?<x>([^""]|"""")+)""|""(?<x>)""|(?<x>[^\t]+))\t?",
        //                RegexOptions.ExplicitCapture);

        // Correction according to Cesar Morigaki from SO question: https://mcmap.net/q/724347/-wpf-datagrid-pasting/5436437?noredirect=1#comment115043404_5436437
        var matchesTab = Regex.Matches(line, @"[\r\n\f\v ]*?((?<x>(?=[\t]+))|""(?<x>([^""]|"""")+)""|""(?<x>)""|(?<x>[^\t]+))\t?",
             RegexOptions.ExplicitCapture);

        string[] values = (from Match m in matchesTab
                            select m.Groups["x"].Value.Trim().Replace("\"\"", "\"")).ToArray();

        return values;
    }

        // ******************************************************************
        public static string[] ParseLineSemicolonSeparated(string line)
        {
            // CSV line parsing : From "jgr4" in http://www.kimgentes.com/worshiptech-web-tools-page/2008/10/14/regex-pattern-for-parsing-csv-files-with-embedded-commas-dou.html
            var matches = Regex.Matches(line, @"\s?((?<x>(?=[;]+))|""(?<x>([^""]|"""")+)""|""(?<x>)""|(?<x>[^;]+));?",
                                        RegexOptions.ExplicitCapture);

            string[] values = (from Match m in matches
                               select m.Groups["x"].Value.Trim().Replace("\"\"", "\"")).ToArray();

            return values;
        }

        // ******************************************************************
        public static string[] ParseLineNotSeparated(string line)
        {
            string [] lineValues = new string[1];
            lineValues[0] = line;
            return lineValues;
        }

        // ******************************************************************
        public static List<string[]> ParseText(string text)
        {
            string[] lines = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            return ParseString(lines);
        }
        
        // ******************************************************************
        public static List<string[]> ParseString(string[] lines)
        {
            List<string[]> result = new List<string[]>();

            LineSeparator lineSeparator = LineSeparator.Unknown;
            if (lines.Any())
            {
                lineSeparator = GuessCsvSeparator(lines[0]);
            }

            Func<string, string[]> funcParse = DictionaryOfLineSeparatorAndItsFunc[lineSeparator];

            foreach (string line in lines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                result.Add(funcParse(line));
            }

            return result;
        }

        // ******************************************************************
    }
}

ClipboardHelper2.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.IO;
using System.Linq;
using System.Windows.Forms.VisualStyles;
using HQ.Util.General.CSV;

namespace HQ.Util.General.Clipboard
{
    // Uses Clipboard in WPF (PresentationCore.dll in v4 of the framework)
    public static class ClipboardHelper2
    {
        public delegate string[] ParseFormat(string value);

        public static List<string[]> ParseClipboardData()
        {
            List<string[]> clipboardData = new List<string[]>();

            // get the data and set the parsing method based on the format
            // currently works with CSV and Text DataFormats            
            IDataObject dataObj = System.Windows.Clipboard.GetDataObject();

            if (dataObj != null)
            {
                string[] formats = dataObj.GetFormats();
                if (formats.Contains(DataFormats.CommaSeparatedValue))
                {
                    string clipboardString = (string)dataObj.GetData(DataFormats.CommaSeparatedValue);
                    {
                        // EO: Subject to error when a CRLF is included as part of the data but it work for the moment and I will let it like it is
                        // WARNING ! Subject to errors
                        string[] lines = clipboardString.Split(new string[] { "\r\n" }, StringSplitOptions.None);

                        string[] lineValues;
                        foreach (string line in lines)
                        {
                            lineValues = CsvHelper.ParseLineCommaSeparated(line);
                            if (lineValues != null)
                            {
                                clipboardData.Add(lineValues);
                            }
                        }
                    }
                }
                else if (formats.Contains(DataFormats.Text))
                {
                    string clipboardString = (string)dataObj.GetData(DataFormats.Text);
                    clipboardData = CsvHelper.ParseText(clipboardString);
                }
            }

            return clipboardData;
        }
    }
}
Argenteuil answered 25/3, 2011 at 18:15 Comment(3)
really a good one. mostly works but sometimes I get a crash on the line CommitEditCommand.Execute(this,this); and crash message is "Input string was not in a correct format.". this I get when I use a combobox and if it has no element selected. Although I used MVVM patern, It will expect something at the time of paste. But putting a try catch around will resolve my problem.Serow
I added my current code. I suppose I got some problems like you because there is pretty big changes. This one should work, I hope. Note that CSV format has sometimes different flavors (the separator could change : "," ";", tab, etc). Actually the new code provided is used in our production code and seems to work ok. Good luck !Argenteuil
It worked pretty well for me. I recommend to check @Cesar Morigaki solution for blank cells from excel.Imhoff
V
5

For those interested - there does seem to be something going wrong with the columns attempt to update the value of the bindable object - possible data type conversion so i have implemented this myself and it works like a charm now.

protected virtual void OnExecutedPaste(object sender, ExecutedRoutedEventArgs args)
    {
        // parse the clipboard data
        List<string[]> 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 <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
        {
            if (CanUserAddRows && 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 < 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 < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
            {
                DataGridColumn column = ColumnFromDisplayIndex(j);
                string propertyName = ((column as DataGridBoundColumn).Binding as Binding).Path.Path;
                object item = Items[i];
                object value = rowData[rowDataIndex][columnDataIndex];
                PropertyInfo pi = item.GetType().GetProperty(propertyName);
                if (pi != null)
                {
                   object convertedValue = Convert.ChangeType(value, pi.PropertyType);
                    item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null);                    
                }
                //column.OnPastingCellClipboardContent(item, rowData[rowDataIndex][columnDataIndex]);
            }
        }

}

Vincevincelette answered 7/11, 2010 at 17:7 Comment(0)
S
3

Small correction in CsvHelper.cs from @eric-ouellet answer. When copying from Excel with any blank cell, the data mapping doesn't apply correctly. I modified the Regex for ParseLineTabSeparated method.

public static string[] ParseLineTabSeparated(string line)
{
    var matchesTab = Regex.Matches(line, @"[\r\n\f\v ]*?((?<x>(?=[\t]+))|""(?<x>([^""]|"""")+)""|""(?<x>)""|(?<x>[^\t]+))\t?",
                    RegexOptions.ExplicitCapture);

    string[] values = (from Match m in matchesTab
                       select m.Groups["x"].Value.Trim().Replace("\"\"", "\"")).ToArray();

    return values;
}
Shut answered 31/8, 2018 at 17:45 Comment(0)
M
0

If I'm right you should also need a range check for variable j:

            for (int j = minColumnDisplayIndex; j <= maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
            {
                if (j == maxColumnDisplayIndex)
                    continue;
                DataGridColumn column = ColumnFromDisplayIndex(j);
                column.OnPastingCellClipboardContent(Items[i], rowData[rowDataIndex][columnDataIndex]);
            }

Otherwise you get an ArgumentOutOfRangeException when pasting content with more columns your grid actually can hold.

Marconigraph answered 1/7, 2012 at 9:14 Comment(0)
P
0

https://www.codeproject.com/Articles/1172338/Easy-WPF-Copy-Paste-Excel

The link above gives the "Paste into DataGrid" functionality with a Nuget package.

Bool type support is missing. I had to download the source and modify convertes.cs.

 case "System.Boolean": resultado = bool.Parse(valor); break;
 if (tipoDelNulable.Contains("System.Boolean")) resultado = string.IsNullOrEmpty(valor) ? null : (bool?)bool.Parse(valor);
Paw answered 5/4, 2019 at 10:46 Comment(0)
L
0

I had a similar issue where I wanted to paste columns copied from Excel (in clipboard) to a datagrid in WPF. My approach was to construct a datatable and set the set the datagrid datacontext to this datatable.

            string s = Clipboard.GetText();

            string[] lines = s.Split('\r');
            DataTable dt = new DataTable();

            string[] fields;
            int row = 0;
            int col = 0;

            foreach (string item in lines)
            {
                dt.Rows.Add();
                fields = item.Split('\t');
                foreach (string f in fields)
                {
                    dt.Rows[row][col] = f;
                    col++;
                }
                row++;
                col = 0;
            }
Lekishalela answered 23/11, 2021 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.