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

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

5 Answers 202 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrea
Top achievements
Rank 1
Andrea asked on 06 Jul 2016, 10:09 AM

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;
   }
}

5 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 06 Jul 2016, 11:59 AM
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.
0
Andrea
Top achievements
Rank 1
answered on 06 Jul 2016, 12:34 PM

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.

 

0
Dimitar
Telerik team
answered on 06 Jul 2016, 12:52 PM
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.
0
Andrea
Top achievements
Rank 1
answered on 06 Jul 2016, 01:58 PM

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?

 

 

0
Dimitar
Telerik team
answered on 07 Jul 2016, 09:02 AM
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.
Tags
GridView
Asked by
Andrea
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Andrea
Top achievements
Rank 1
Share this question
or