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

Pasting from Excel won't set GridViewComboBoxColumn

7 Answers 50 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sandhiya
Top achievements
Rank 1
Sandhiya asked on 11 Jul 2013, 04:45 PM
I'm trying to paste from Excel to RadGridView.
Everything works fine, except GridViewComboBoxColumn will become empty (after paste).
I have create a sample project to reproduce this behaviour, but I only can upload image files.
I'm using Telerik.Windows.Controls.GridView ver 2012.3.1129.1050

public MainPage()
{
    InitializeComponent();
    GridView.ClipboardCopyMode = GridViewClipboardCopyMode.Cells | GridViewClipboardCopyMode.Header;
    GridView.ClipboardPasteMode = GridViewClipboardPasteMode.Cells | GridViewClipboardPasteMode.SkipFirstLine | GridViewClipboardPasteMode.OverwriteWithEmptyValues;
}
 
private void GridView_PastingCellClipboardContent(object sender, Telerik.Windows.Controls.GridViewCellClipboardEventArgs e)
{
    if (e.Cell.Column.UniqueName == "Model")
    {
        var modelCode = e.Value.ToString().Trim();
        var model = (LayoutRoot.DataContext as ViewModel).Models.FirstOrDefault(m => m.Code == modelCode);
        if (model != null)
        {
            e.Value = model;
        }
        else
        {
            e.Cancel = true;
        }
    }
}

7 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 15 Jul 2013, 07:08 AM
Hello Sandhiya,

I have tested the behavior on our demos and everything works as expected.
Please keep in mind that pasting updates the values according to the property used in DataMemberBinding and the value to be pasted should be the same type and the source of the column should have a value corresponding to the one being pasted.  


Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Sandhiya
Top achievements
Rank 1
answered on 15 Jul 2013, 08:12 AM
Hi Maya,

I have uploaded a sample project to reproduce this behaviour, you can download & try it.
Actually I was trying sample code from telerik documentation here.

Regards,
Sandhiya
0
Maya
Telerik team
answered on 15 Jul 2013, 08:25 AM
Hello,

That would be the expected behavior:
1. GridViewComboBoxColumn - you need to bind to a single property rather than a complex object since when pasting, you are pasting a value corresponding to a property of that business object rather than just a single property.
2. Read-only columns - you cannot paste on them by default. You need to handle the pasting events and update the values manually. 

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Sandhiya
Top achievements
Rank 1
answered on 15 Jul 2013, 09:26 AM
Hi Maya, 

Can you more elaborate on point #1, I think I'm lost on there.
For point #2, yes I know it and my code doesn't paste for readonly columns.

Let me explain the case more details:
1. In Excel data there is a column named "Model Code" with type of string.
2. I have an object "Model" with properties id (int), code (string), name (string), colorCode (string), and colorName (string).
3. 2nd, 3rd, and 4th columns of GridView bound to model.name, model.colorCode, and model.colorName.
4. 1st column of GridView (editable comboBox column) bound to whole model object, with CellTemplate contains a textblock that bound to model.Code
5. When user paste data from Excel to GridView, my code search a model from Models Collection by model.Code. And if found, it pass the model to GridViewCellClipboardEventArgs.value
0
Accepted
Maya
Telerik team
answered on 16 Jul 2013, 06:41 AM
Hello Sandhiya,

You can try binding the first column just to a single property - not an entire object - and bind DataMemberBinding to the property you want - I guess it is the id of the model. Then when you paste the value from the first column from Excel - "Model Code", everything should work correctly.
Please check out our documentation for a reference on how to work with GridViewComboBoxColumn. 

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Sandhiya
Top achievements
Rank 1
answered on 19 Jul 2013, 04:37 AM
Hi Maya, thanks for your solution.
Unfortunately we can't use your solution since we are re-using entities from middle tier.
And those entities only have reference to a whole other entity object (not their ID).
For example, to model object (not model ID).
After review our code, we will have to change a lot of code for this case.

Then we try to find some workaround, and below code is works.
Maybe it's some kind of a hack, but it's working :)

private void GridView_PastingCellClipboardContent(object sender, Telerik.Windows.Controls.GridViewCellClipboardEventArgs e)
{
    if (e.Cell.Column.UniqueName == "Model")
    {
        var modelCode = e.Value.ToString().Trim();
                var model = (LayoutRoot.DataContext as ViewModel).Models.FirstOrDefault(m => m.Code == modelCode);
        if (model != null)
        {
            var unit = e.Cell.Item as Unit;
            unit.Model = model;
        }
 
        e.Cancel = true; //a little hack for RadGridView, otherwise value will be set to null again.
    }
}
0
Maya
Telerik team
answered on 19 Jul 2013, 06:32 AM
Hi Sandhiya,

Your solution looks ok. I am glad that you have resolved the issue on your side. 

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Sandhiya
Top achievements
Rank 1
Answers by
Maya
Telerik team
Sandhiya
Top achievements
Rank 1
Share this question
or