Declined
Last Updated: 19 Jul 2022 10:52 by ADMIN
Martin
Created on: 18 Jul 2022 12:43
Category: UI for WinForms
Type: Bug Report
0
DataGridView with DataTable skips strings that are to long while pasting.

Report steps:

  1. Make a datatable with a string-column with a max. length of 10 characters.
  2. Will the datatable with some data.
  3. Bind it to a RadGridView
  4. In Excel (or another applicatie) create a now table with some strings, this time longer that 10 characters.
  5. Paste this data from Excel to the RadGridView

Expected behavior:

  1. The strings that are to long will either cause an exception or they are truncated.

Observed behavior:

  1. The string with the value that is to long is skipped during pasting.

Extra detail:

In the method MasterGridViewTemplate.PasteDataToRow the next piece is code is located:

if (CurrentView.ViewTemplate.Columns[columnIndex].DataType == typeof(string))
{
    if (column is GridViewTextBoxColumn viewTextBoxColumn && viewTextBoxColumn.MaxLength > 0 && rowData[rowIndex].Length > viewTextBoxColumn.MaxLength)
        obj = rowData[rowIndex].Substring(0, viewTextBoxColumn.MaxLength);
}
The property viewTextBoxColumn.MaxLength returns 32767 (and not 10) which is clearly a related and possibly a separate bug.
1 comment
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 19 Jul 2022 10:52

Hello, Martin,  

I was able to reproduce the described behavior if there is MaxLength set to the respective column for the DataTable, but not to the respective column in RadGridView. Since the DataTable can't accept text longer than the maximum, in a normal situation it is not possible to set a value to the DataTable that violates the maximum:

The above error is handled and the user doesn't see the message. However, RadGridView offers the DataError event which is expected to be fired in this situation if the notifications are enabled. As a result, the user can be notified if this is the required behavior: 

        public RadForm1()
        {
            InitializeComponent();
       
            DataTable dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns["Title"].MaxLength = 10;
            dt.Rows.Add("test");
            this.radGridView1.DataSource = dt;
            GridViewTextBoxColumn titleColumn = this.radGridView1.Columns["Title"] as GridViewTextBoxColumn;
            //  titleColumn.MaxLength = 10;

            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

            this.radGridView1.ClipboardPasteMode = GridViewClipboardPasteMode.EnableWithNotifications;
            this.radGridView1.DataError += radGridView1_DataError;
        }

        private void radGridView1_DataError(object sender, GridViewDataErrorEventArgs e)
        {
            RadMessageBox.Show(e.Exception.Message); 
        }

An alternative approach is to specify the GridViewTextBoxColumn.MaxLength and thus the pasted value will be trimmed: 

            DataTable dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns["Title"].MaxLength = 10;
            dt.Rows.Add("test");
            this.radGridView1.DataSource = dt;
            GridViewTextBoxColumn titleColumn = this.radGridView1.Columns["Title"] as GridViewTextBoxColumn;
            titleColumn.MaxLength = 10;

This is the achieved result:

Please give this approach a try and see how it works for your scenario.

Regards,
Dess | Tech Support Engineer, Principal
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.