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
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.
Maya
Telerik
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 >>
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
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.
Maya
Telerik
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 >>
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
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.
Maya
Telerik
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 >>
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.
}
}
Your solution looks ok. I am glad that you have resolved the issue on your side.
Maya
Telerik
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 >>