How-To Replace Row DataBoundObject with Derived Object?

3 posts, 0 answers
  1. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 05 Jan Link to this post

    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. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 06 Jan Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Paul
    Paul avatar
    26 posts
    Member since:
    Aug 2015

    Posted 06 Jan in reply to Dess Link to this post

    I used a bit of a different method, but CellEndEdit event was the key. Thank you!
Back to Top