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

Clone row in RadGridView

4 Answers 142 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hưng Phạm
Top achievements
Rank 1
Hưng Phạm asked on 11 Jul 2010, 05:34 AM
Hi Telerik
I have a web application, it work as below:
I have a object : Customer and list data of  Customer : ObservableCollection<Customer>, it inherit from INotifyPropertyChanged
and I have a RadGridView, i implemented binding between RadGridVIew and ObservableCollection<Customer> with mode binding twoway.
I want to have clone of row in RadGridView, then it will be going to insert to last row with command as below:
GridView.Items.Insert(GridView.Items.Count,GridVIew.SelectedItem); or GridView.Items.Add(GridVIew.SelectedItem);
But seemed to do it somehow. I received exception as: "operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead"
So, i have to how to implement clones a row on RadGridView.
example:
Cus_ID       Cus_Name
Cus01         Radda
Cus02         Minda    => this is row, which i selected to implement clone
__________________
and I want clone to second row as below:
Cus_ID       Cus_Name
Cus01         Radda
Cus02         Minda
Cus02         Minda       => this is row, it is copied form from second row.
in this example, I don't specific object as Customer, i want to use SelectedItem property to clones. I hope you can understand my thinking about this issue.

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 12 Jul 2010, 07:27 AM
Hello Hưng Phạm,

 You need to insert this item directly in ItemsSource and the grid will respond immediately! Working with Items directly is possible if you have not set ItemsSource property. 

Kind regards,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Hưng Phạm
Top achievements
Rank 1
answered on 13 Jul 2010, 04:33 AM
Hi admin
I worked as you, and it work fine. but it appear a new issue, i cannot process it. As below:
i have a command as:
object TEMP;
public virtual void Clone()
{
    try
    {
        int index = (this.DataItems as IList).IndexOf(gridview.CurrentItem);
        TEMP = new object();
        TEMP = (this.DataItems as IList)[index];
        (this.DataItems as IList).Insert((this.DataItems as IList).Count, TEMP);
        gridview.SelectedItem = TEMP;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
it added clones from row which i selected. But when i edit row which I cloned, all row which cloned, it too. what can I do?
I want when clones a row and edit it, it only edit row which i selected, don't have to all row cloned. 
0
Hưng Phạm
Top achievements
Rank 1
answered on 13 Jul 2010, 09:52 AM
...
?
0
Milan
Telerik team
answered on 13 Jul 2010, 01:31 PM
Hi Hưng Phạm,

Unfortunately this kind of cloning will confuse our selection mechanism. Currently we use keys to identify selected items and if two items have the same hash code (GetHashCode method) and their Equals method returns true the grid cannot distinguish between the items and ultimately this manifests as strange selection behavior. 

Maybe you should try to clone the items by creating new instance and duplicating the property values.

Greetings,

Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Hưng Phạm
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Hưng Phạm
Top achievements
Rank 1
Milan
Telerik team
Share this question
or