Add new row on a grid bound to an object which does not have a parameterless constructor

6 posts, 0 answers
  1. Andrea
    Andrea avatar
    52 posts
    Member since:
    Oct 2012

    Posted 06 Jul Link to this post

    Hello, i have this, were working using microsoft grid, how can i make it work with the winforms grid?

    When i try to add a new row raise an exception like 'System.MissingMethodException' no parameterless constructor.

    [Serializable]
    public class ScheduledMovement : NotifyPropertyChanged
    {
        //various props
        public excMovements Movements
          {get;set;}
        //other
    }
     
    public class excMovements :
      BindingList<ScheduledMovementDetail>, IBindingList
    {
      protected override object AddNewCore()
      {
          ScheduledMovementDetail n = new ScheduledMovementDetail(parent);
          Add(n);
          return n;
       }
    }

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 06 Jul Link to this post

    Hi Andrea,

    Thank you for writing.

    I have tested with similar structure, but there is no exception on my side. I have attached my test project. Could you please let me know what I need to change in order to reproduce the exception.

    I am looking forward to your reply.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Andrea
    Andrea avatar
    52 posts
    Member since:
    Oct 2012

    Posted 06 Jul in reply to Dimitar Link to this post

    Hi, using your project i can generate the exceptio with this modification: add a constructor with a parameter

     

    class MyList : BindingList<ChildData>, IBindingList
       {
           protected override object AddNewCore()
           {
               ChildData item = new ChildData(0);
               item.ChildText = "test";
               Add(item);
               return item;
           }
       }
       class ChildData
       {
           public ChildData(int x)
           { }
           public string ChildText { get; set; }
       }

    that's it.

    when you try to add a new row on the child template, the exception is generatd.

     

  5. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 06 Jul Link to this post

    Hi Andrea,

    Thank you for writing back.

    In this case, the parameterless constructor is required by our implementation. We use it to create a new instance of the class and populate the data according to the data in the columns. So in order to be able to add new rows you need to add a parameterless constructor as well. 

    I hope that you find this information useful. 

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  6. Andrea
    Andrea avatar
    52 posts
    Member since:
    Oct 2012

    Posted 06 Jul in reply to Dimitar Link to this post

    Ok thanks.  It is possible in any way to use an external factory to let the grid create the instance or to have an event to capture to let me create the object using the newRowInfo as parameter?

     

     

  7. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 07 Jul Link to this post

    Hello Andrea,

    Thank you for writing.

    You can use the UserAddingRow event to cancel the adding operation and then manually add new row directly to your data source. For example:
    private void RadGridView1_UserAddingRow(object sender, GridViewRowCancelEventArgs e)
    {
        if (e.Rows[0].ViewTemplate == radGridView1.MasterTemplate.Templates[0])
        {
            e.Cancel = true;
            radGridView1.CurrentRow = null;
            var item = data[0].Data.AddNew();
            
        }
    }

    Please let me know if there is something else I can help you with. 
     
    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready