Pasting event on RadGridView firing twice

2 Answers 187 Views
GridView
Stephen
Top achievements
Rank 1
Iron
Iron
Stephen asked on 25 Aug 2021, 05:58 PM

I have a radgridview that I've enabled pasting.  Everything works great but for some reason when I right click and hit paste no matter how many rows I'm coping the pasting event fires twice thus duplicating the input rows.

private void RgvSup_Pasting(object sender, Telerik.WinControls.UI.GridViewClipboardEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                // paste row data
                if (RgvSup.CurrentRow is GridViewNewRowInfo)
                {
                    if (Clipboard.GetText() != string.Empty && Clipboard.GetText() != "")
                    {
                        string[] lines = Clipboard.GetText().Split(new string[] { "\r\n" }, StringSplitOptions.None);
                        try
                        {
                            foreach (string rowInfo in lines)
                            {
                                string[] cellVal = rowInfo.Split('\t');
                                if (cellVal.Length >= 9)
                                {
                                    SUPPLIER ObjSup = new SUPPLIER();
                                    try { ObjSup.SUP_NAME = cellVal[0]; } catch { }
                                    if (cellVal[1].Contains(";"))
                                        cellVal[1] = cellVal[1].Split(';').GetValue(0).ToString();
                                    try { ObjSup.SUP_PUL_CAT_ID = decimal.Parse(cellVal[1]); } catch { ObjSup.SUP_PUL_CAT_ID = null; }
                                    try { ObjSup.SUP_PHONE = ClsCommon.SetPhone(cellVal[2]); } catch { }
                                    try { ObjSup.SUP_PCONT_NAME = cellVal[3]; } catch { }
                                    try { ObjSup.SUP_PCONT_CCELL = ClsCommon.SetPhone(cellVal[4]); } catch { }
                                    try { ObjSup.SUP_ADDRESS = cellVal[5]; } catch { }
                                    try { ObjSup.SUP_CITY = cellVal[6]; } catch { }
                                    if (cellVal[7].Contains(";"))
                                        cellVal[7] = cellVal[7].Split(';').GetValue(0).ToString();
                                    try { ObjSup.SUP_ST_ID = decimal.Parse(cellVal[7]); } catch { ObjSup.SUP_ST_ID = null; }
                                    try { ObjSup.SUP_ZIP = ClsCommon.SetZip(cellVal[8]); } catch { }
                                    ObjSup.SUP_CL_ACTIVE_ID = 1;

                                    MstEnt.eSUPPLIER.Add(ObjSup);
                                }
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Incompatible DataSources");
                        }

                        ClsCommon.SaveMstEnt(MstEnt, "RpurfEditSup.cs", "RgvSup_Pasting");
                        RefreshData();
                        MessageBox.Show("Completed");
                    }
                }
            }
            catch (Exception Ex)
            {
                ClsCommon.WriteAppError(Ex, "RpurfEditSup.cs", "RgvSup_Pasting");
            }
            Cursor.Current = Cursors.Default;
        }

 

2 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Aug 2021, 10:43 AM

Hello, Stephen,

Following the provided information I was not able to reproduce the problem. Pasting event occurs when the grid is about to perform the paste operation and it fires just once on my end. Please check the attached gif file.

public RadForm1()
{
    InitializeComponent();
    this.radGridView1.ClipboardPasteMode = GridViewClipboardPasteMode.Enable;
    this.radGridView1.Pasting += RadGridView1_Pasting;
}

private void RadGridView1_Pasting(object sender, GridViewClipboardEventArgs e)
{
    Console.WriteLine("Pasting");
}

I am providing my test project for your reference. Could you please give it a try and share the exact steps that I should follow to replicate the problem. Thus, I could be able to assist you further.

I am looking forward to your reply.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Stephen
Top achievements
Rank 1
Iron
Iron
commented on 26 Aug 2021, 03:08 PM

So, doing more research it looks like something to do with the data coming from excel.  When I copy rows from the grid and paste it only executes once.  I'll have to look at what's coming in from excel.

Stephen
Top achievements
Rank 1
Iron
Iron
commented on 26 Aug 2021, 03:10 PM

Also, in your gif I see you only doing a single cell, I'm doing multiple rows as a new row only, no editing existing data.
Stephen
Top achievements
Rank 1
Iron
Iron
commented on 26 Aug 2021, 04:19 PM | edited

When I copy from the grid and paste the end of the string ends with the last cells data.  When I copy from excel the end of the string ends with the "\r\n".

I've found a way to not duplicate the records until I can get an answer why it executes twice.  I put the below code to return if the string ends with "\r\n".

string StrVal = Clipboard.GetText();
                    if (StrVal.Length > 3 && StrVal.Substring(StrVal.Length - 2) == "\r\n" && !BlnFirst)
                    {
                        BlnFirst = true;
                        return;
                    }
                    else
                    {  // existing code adding records.

                   }

0
Nadya | Tech Support Engineer
Telerik team
answered on 30 Aug 2021, 01:29 PM

Hello, Stephen,

The Pasting event is triggered for each of the DataFormats: Html, CommaSeparatedValue, and Text. So, if you are pasting formatted text from Excel the Pasting event would fire for each of the formats. Through GridViewClipboardEventArgs you have access to the data format:

private void RadGridView1_Pasting(object sender, GridViewClipboardEventArgs e)
{
    if (e.Format == "Text")
    {
        // TO DO
    }
}

I hope this information is useful. If you have any other questions please let me know.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Stephen
Top achievements
Rank 1
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or