TrueDBGrid: Line Breaks after copying Data from Excel

Posted by: CEymer on 14 April 2025, 5:46 am EST

  • Posted 14 April 2025, 5:46 am EST

    Hi All,

    We use the TrueDBGrid in our applications for almost 25 years now. For a few months now we are observing a change in behaviour of the grid, that causes us very big headaches…

    Our users copy data from Excel into our applications from time to time. Excel has got the bad habit of ending every value with a line break, even when copying values from one single cell. I’m very sure, that in the past the TrueDBGrid removed these line breaks when inserting data from excel.

    Now we discover more and more strings in our database with line breaks. Exports to csv files suddenly contain these line breaks.

    Could you please have a look into this? How can we get back the old behaviour of the Grid? This is very serious for us.

    Thank you very much for help,

    Christian

  • Posted 15 April 2025, 2:23 am EST

    Hello Christian,

    You can handle KeyDown event of the TrueDBGrid to replace the “/r/n” string in the clipboard text as follows:

    private void C1TrueDBGrid1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            var text = Clipboard.GetText().Replace("\r\n", "\t");
            if (c1TrueDBGrid1.EditActive && c1TrueDBGrid1.Editor is TextBox textBoxEditor)
                textBoxEditor.Text = text;
            else
                c1TrueDBGrid1[c1TrueDBGrid1.Row, c1TrueDBGrid1.Col] = text;
            e.Handled = true;
            e.SuppressKeyPress = true;
        }
    }

    Please refer to the attached sample for implementation. (see TrueDBGrid_CopyDataFromExcel.zip)

    Please let us know if it helps and the previous version of TrueDBGrid you were using (that you are comparing the behavior with).

    Regards,

    Uttkarsh.

  • Posted 22 April 2025, 3:05 am EST

    Hi Uttkarsh,

    Thank you very much for your kind reply. How can I handle the insert over the context menu of the cell’s textbox?

    I looked into the problem further: Six months ago, I updated the Grid form version 2023v3(631) to version 2024v1(657). And I changed the obsolete

    .Style.WrapText = false

    of the DisplayColumn to

    .Style.Wrap = C1.Win.C1TrueDBGrid.TextWrapping.NoWrap
    .

    To prevent further damage to our database I’ve rolled these changes back.

    Kind regards,

    Christian

  • Posted 23 April 2025, 2:58 am EST

    Hello Christian,

    We’re sorry but there is no direct way to intercept paste actions performed via the context menu. You can create a custom TextBox to intercept paste messages and modify them accordingly as follows:

    public class MyTextBox : TextBox
    {
        private const int WM_PASTE = 0x0302;
    
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_PASTE)
            {
                if (Clipboard.ContainsText())
                {
                    string clipboardText = Clipboard.GetText();
    
                    // Modify the clipboard text as needed
                    string modifiedText = ModifyClipboardText(clipboardText);
    
                    // Insert the modified text at the current selection
                    this.SelectedText = modifiedText;
                }
    
                // Prevent the default paste behavior
                return;
            }
    
            base.WndProc(ref m);
        }
    
        private string ModifyClipboardText(string text)
        {
            // modify text
            return text.Replace("\r\n", "\t");
        }
    }

    Then use this textbox as the editor for TrueDBGrid.

    Please refer to the attached modified sample for implementation. (see TrueDBGrid_CopyDataFromExcel_Mod.zip)

    Regards,

    Uttkarsh.

  • Posted 29 April 2025, 8:52 am EST

    Hi Uttkarsh,

    Thank you very much for your reply. I rolled back my applications to version 2023v3(631) of the grid and to Style.WrapText = false – and the problem is gone. Is this change in behaviour intentional in newer versions of the grid?

    Unfortunately, we have hundreds of instances of the grid in our application with thousands of columns. Adding a C1TrueDBGrid1_KeyDown event handler to every instance and replacing the TextBoxes of all cells is impossible for us.

    Could you please have a look into version 2023v3(631) of the Grid compared to newer versions and into the behaviour of Style.Wrap = C1.Win.C1TrueDBGrid.TextWrapping.NoWrap? Could you please bring back the old behaviour? Maybe through a compatibility setting or property?

    This prevents us from updating our ComponentOne Enterprise editions to the newest version and renders our subscription moot.

    Kind regards,

    Christian

  • Posted 30 April 2025, 8:01 am EST

    Hello Christian,

    We tested the behavior using version 2023v3 (build 631) but couldn’t identify the specific change you’re referring to.

    Please refer to the attached video for our version comparison. Let us know if we’re missing any key steps or setup.

    Video: https://drive.mescius.io/download?file=ExternalShare/WinForms/Videos/behaviorComparison.zip

    Regards,

    Uttkarsh.

  • Posted 8 May 2025, 9:16 am EST

    Hi Uttkarsh,

    Thank you for your kind reply and for your video. I’m looking further into it, but at the moment I don’t know which steps are different between our system and your setup. To prevent the line breaks from the strings in the future I have derived a subclass from the C1TrueDBGrid and I have overridden the following method:

            protected override void OnBeforeColUpdate(BeforeColUpdateEventArgs e)
            {
                if ((e.Column.Style.Wrap == C1.Win.C1TrueDBGrid.TextWrapping.NoWrap) && (e.Column.DataColumn.Value is string))
                    e.Column.DataColumn.Value = e.Column.DataColumn.Value.ToString().TrimEnd('\r', '\n');
    
                base.OnBeforeColUpdate(e);
            }

    I apologize for the trouble I caused. Thanks again.

    Kind regards

    Christian

  • Posted 13 May 2025, 12:09 am EST

    Hello Christian,

    It seems you have worked around the behavior. Please let us know if you need anything else or if you are able to replicate the behavior in our sample.

    Regards,

    Uttkarsh.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels