This is a migrated thread and some comments may be shown as answers.

Copy drops TABLE/TR tags when first/last column are hidden

3 Answers 19 Views
GridView
This is a migrated thread and some comments may be shown as answers.
JTaylor
Top achievements
Rank 1
JTaylor asked on 22 Apr 2014, 05:52 PM

I am trying to copy and paste rows from my RadGridView to Excel. It works as expected when the first and last columns are visible, but if the first column is hidden the opening TABLE/TR tags in the HTML format on the clipboard are lost, and if the last column is hidden the matching closing tags are lost.

Repro: (VS2012, RadGridView Q1 2014)
Add a datasource with three columns. Right click on row header -> Copy, Paste into Excel.
Expected: The data spans three columns.
Actual: The data spans three columns. Yay!
Clipboard contents:

Version:1.0
StartHTML:00000097
EndHTML:00000239
StartFragment:00000133
EndFragment:00000203
<HTML>
<BODY>
<!--StartFragment--><TABLE><TR><TD>3</TD><TD>Tools</TD><TD>Electronics</TD></TR></TABLE>
<!--EndFragment-->
</BODY>
</HTML>


Hide the first column. Right click on row header -> Copy, Paste into Excel.
Expected: The data spans two columns.
Actual: All data is in a single cell.
Clipboard contents: No <TABLE><TR>.

Version:1.0
StartHTML:00000097
EndHTML:00000216
StartFragment:00000133
EndFragment:00000180
<HTML>
<BODY>
<!--StartFragment--><TD>Books</TD><TD>Chocolate</TD></TR></TABLE>
<!--EndFragment-->
</BODY>
</HTML>

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Apr 2014, 05:59 AM
Hello Jovana,

Thank you for writing.

I was able to reproduce the problem that you are facing with the hidden first/last column. I confirm that it is an issue with our RadGridView and its Copy functionality. I have logged it in our Feedback Portal. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - Feedback Item.

I have also updated your 
Telerik points.

Currently, the possible solution that I can suggest is to create a derivative of the RadGridView and replace the wrong content in the Clipboard:
public class CustomGrid : RadGridView
{
    protected override RadGridViewElement CreateGridViewElement()
    {
        return new CustomRadGridViewElement();
    }
}
 
public class CustomRadGridViewElement : RadGridViewElement
{
    protected override MasterGridViewTemplate CreateTemplate()
    {
        return new CustomMasterGridViewTemplate();
    }
}
 
public class CustomMasterGridViewTemplate : MasterGridViewTemplate
{
    public override void Copy()
    {
        base.Copy();
         
        if (Clipboard.ContainsData(DataFormats.Html))
        {
            string data = Clipboard.GetData(DataFormats.Html).ToString();
            StringBuilder sb = new StringBuilder(data);
            if (!data.Contains("<TABLE>"))
            {
                int insertIndex = data.IndexOf("<TD>");
                sb.Insert(insertIndex, "<TABLE><TR>");
            }
            else if (!data.Contains("</TABLE>"))
            {
                int insertIndex = data.LastIndexOf("<TD>");
                sb.Insert(insertIndex, "</TR></TABLE>");
            }
             
            Clipboard.SetData(DataFormats.Html, sb.ToString());
        }
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
JTaylor
Top achievements
Rank 1
answered on 28 Apr 2014, 08:51 PM
Thank you for adding the feedback item.
The workaround you provided doesn't cover the case when there are multiple rows copied: the <TR> tag will still be missing on rows other than the first row, similarly the </TR> tag will be missing from all but the last row.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 May 2014, 06:33 AM
Hello Jovana,

Thank you for writing back.

When I select several rows, copy them and paste in a sheet in MS Excel, all the rows are inserted correctly, although the contained string in the Clipboard does not include all necessary <TR> tags. However, in order to handle the case when the user copies multiple rows (MultiSelect property is set to true), it is necessary to modify the sample code snippet and insert the missing "<TR>" tags as follows:  
public override void Copy()
{
    base.Copy();
     
    if (Clipboard.ContainsData(DataFormats.Html))
    {
        string data = Clipboard.GetData(DataFormats.Html).ToString();
        StringBuilder sb = new StringBuilder(data);
        if (data.Contains("<TD>"))
        {
            if (!data.Contains("<TABLE>"))
            {
                int insertIndex = data.IndexOf("<TD>");
                sb.Insert(insertIndex, "<TABLE><TR>");
            }
            else if (!data.Contains("</TABLE>"))
            {
                int insertIndex = data.LastIndexOf("<TD>");
                sb.Insert(insertIndex, "</TR></TABLE>");
            }
 
            int startIndex = sb.ToString().IndexOf("</TR>");
            int endIndex = sb.ToString().LastIndexOf("</TR>");
         
            while (startIndex < endIndex && startIndex > -1)
            {
                sb.Insert(startIndex + "</TR>".Count(), "<TR>");
                startIndex = sb.ToString().IndexOf("</TR>", startIndex + 1);
            }
         
            Clipboard.SetData(DataFormats.Html, sb.ToString());
        }
    }
}

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
JTaylor
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
JTaylor
Top achievements
Rank 1
Share this question
or