Copy And Paste between 2 gridview with different columns

2 posts, 0 answers
  1. Dario
    Dario avatar
    61 posts
    Member since:
    Apr 2011

    Posted 29 May 2015 Link to this post

    Hi to all,

    I trying to copy from one gridview that has 4 columns and paste another gridview that has 3 columns.

    Now, in RadGridViewPasting of destination GridView I wrote this code, those gridviews are into Windows dialog that called from main Windows with ShowDialog mode. But this code generate an error.

     If I compare ContainsData in incoming and ContainsData in outcoming are formally correct.

    What's up?

    01.private void MappingRadGridViewPasting(object sender, GridViewClipboardEventArgs e)
    02.        {
    03.            try
    04.            {
    05.                if (Clipboard.ContainsData(DataFormats.Text))
    06.                {
    07.                    string data = Clipboard.GetData(DataFormats.Text).ToString();
    08.                    if (data != string.Empty)
    09.                    {
    10.                        var items = data.Split('\n');
    11. 
    12.                        StringBuilder sb = new StringBuilder();
    13. 
    14.                        for (int i = 0; i < items.Length; i++)
    15.                        {
    16.                            var fields = items[i].Split('\t');
    17. 
    18.                            int type = Convert.ToInt32(fields[0]);
    19.                            int id = Convert.ToInt32(fields[1]);
    20.                            string name = fields[2];
    21. 
    22.                            if (i == items.Length - 1)
    23.                                sb.AppendFormat("{0}\t{1}\t\t{2}", type, id, name);
    24.                            else
    25.                                sb.AppendFormat("{0}\t{1}\t\t{2}\r\n", type, id, name);
    26.                        }
    27. 
    28.                        string output = sb.ToString();
    29. 
    30.                        Clipboard.SetData(DataFormats.Text, output);
    31.                    }
    32.                }
    33. 
    34.            }
    35.            catch (Exception ex)
    36.            {
    37.                ExceptionHelper.ShowException("MappingRadGridViewPasting", ex);
    38.            }
    39.        }

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 02 Jun 2015 Link to this post

    Hello Dario,

    Thank you for writing.

    RadGridView's Copying and Pasting events are fired once for each supported format: DataFormats.Text, DataFormats.HTML, DataFormats.CommaSeparatedValue. You should set the Clipboard text data when the DataFormats is Text:
    private void radGridView2_Pasting(object sender, Telerik.WinControls.UI.GridViewClipboardEventArgs e)
    {
        try
        {
            if (e.Format == DataFormats.Text && Clipboard.ContainsData(DataFormats.Text))
            {
                string data = Clipboard.GetData(DataFormats.Text).ToString();
                if (data != string.Empty)
                {
                    var items = data.Split('\n');
     
                    StringBuilder sb = new StringBuilder();
     
                    for (int i = 0; i < items.Length; i++)
                    {
                        var fields = items[i].Split('\t');
     
                        int type = Convert.ToInt32(fields[0]);
                        int id = Convert.ToInt32(fields[1]);
                        string name = fields[2];
     
                        if (i == items.Length - 1)
                            sb.AppendFormat("{0}\t{1}\t{2}", type, id, name);
                        else
                            sb.AppendFormat("{0}\t{1}\t\t{2}\r\n", type, id, name);
                    }
     
                    string output = sb.ToString();
     
                    Clipboard.SetData(DataFormats.Text, output);
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    Additionally, you can refer to our Copy/Paste DisplayMember for GridViewComboBoxColumn KB article which demonstrates a sample example how to manipulate the Clipboard data.

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top