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

How-To Replace Row DataBoundObject with Derived Object?

2 Answers 45 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 05 Jan 2016, 04:56 PM

I have a grid populated from a base class. The base class has a enum property to identify what type of derived object it is. When the user adds a new row and changes that enum value, I need to replace the object with the associated derived type. Example:

public enum ObjectType
{
    Base,
    ChildA,
    ChildB
}
 
public class Base
{
    public ObjectType Type { get; set; }
}
 
public class ChildA : Base { }
 
public class ChildB : Base { }

The grid will initially create a new row with a DataBoundObject of Type 'Base'.

When the the user changes the 'ObjectType' column to 'ObjectType.ChildA' I need to replace the row's DataBoundObject with a new instance of 'ChildA'.

The grid's DataSource is set to a BindingSource; who's DataSource is set to BindingList<Base>.

I tried to replace the object in the BindingList. Then set the BindingSource.DataSource to the updated BindingList. And then call BindingSource.ResetBindings(false) to updated the GridView. But I receive a NullReferenceException.

How can I replace the Row's DataBoundObject with a derived object?

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Jan 2016, 11:34 AM
Hello Paul,

Thank you for writing.

You can use the CellEndEdit event and insert a new item in the DataSource with the new type and remove the old one:
BindingList<Base> items = new BindingList<Base>();
 
public Form1()
{
    InitializeComponent();
    
    for (int i = 0; i < 5; i++)
    {
        items.Add(new Base());
    }
    this.radGridView1.DataSource = items;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill; 
    this.radGridView1.CellEndEdit += radGridView1_CellEndEdit;
}
 
private void radGridView1_CellEndEdit(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    int index = -1;
    index = e.RowIndex;
    if ((ObjectType)e.Value == ObjectType.ChildA)
    {
        items.Insert(index, new ChildA());
    }
    else if ((ObjectType)e.Value == ObjectType.ChildB)
    {
        items.Insert(index, new ChildB());
    }
    else
    {
        items.Insert(index, new Base());
    }
 
    this.radGridView1.CurrentRow = this.radGridView1.Rows[index];
    e.Row.Delete(); 
}
 
public enum ObjectType
{
    Base,
    ChildA,
    ChildB
}
 
public class Base
{
    public ObjectType Type { get; set; }
}
 
public class ChildA : Base
{
    public ChildA()
    {
        this.Type = ObjectType.ChildA;
    }
}
 
public class ChildB : Base
{
    public ChildB()
    {
        this.Type = ObjectType.ChildB;
    }
}

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Paul
Top achievements
Rank 1
answered on 06 Jan 2016, 07:11 PM
I used a bit of a different method, but CellEndEdit event was the key. Thank you!
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Paul
Top achievements
Rank 1
Share this question
or