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

C# : Controls / RadDataForm (WPF) - InitializingNewItem : How to set Default Values

4 Answers 213 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 23 Jul 2020, 10:17 AM

 

I am attempting to set default values to a Telerik RadDataForm in the InitializingNewItem Event. RadDataForm is linked to a RadGridView as the Source, the intention is that when an item is selected in the grid and this is present in the dataform, clicking the Add New Item will pre-populate the dataform with some key data from the previously selected item.

You (Telerik) state: "Occurs when a new item is being added but after the AddingNewItem event. You can use this to set initial values for the initialized objects by passing an instance to the InitializingNewItemEventArgs' DataItem property."

However simply using:

private void RadDataForm_InitializingNewItem(object sender, Telerik.Windows.Controls.Data.DataForm.InitializingNewItemEventArgs e)

{

    RadDataForm d = sender as RadDataForm;

    // Copy and set all data to existing record

    e.DataItem = d.CurrentItem as GetParts_Result;

    // Change some data to another default value

    (GetParts_Result)e.DataItem).Name = "New Name";

}

 

This causes a ArgumentNullException error for 'Parameter name: Key'?

Is there a working example of this event being utilised available...

 

 

4 Answers, 1 is accepted

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 28 Jul 2020, 07:30 AM

Hello Matthew,

Thank you for the shared code snippet. 

Indeed you can pass an instance to the DataItem property of the event arguments, however you should make sure to create a new instance. This is important as you should not be populating the ItemsSource multiple times with the same instance. 

I am attaching a sample project, where I have demonstrated this approach for your reference. I do hope you find this helpful. 

Regards,
Vladimir Stoyanov
Progress Telerik

0
Matthew
Top achievements
Rank 1
answered on 28 Jul 2020, 10:56 AM

Hi Vladimir,

Thanks for your response - I have added in an extra line to get closer to what I am attempting to do:

private void myRadDataForm_InitializingNewItem(object sender, Telerik.Windows.Controls.Data.DataForm.InitializingNewItemEventArgs e)
{
    RadDataForm d = sender as RadDataForm;
 
    // Copy and set all data to existing record
 
    e.DataItem = new Employee();
 
    // Change some data to another default value
    e.DataItem = d.CurrentItem as Employee;
 
    ((Employee)e.DataItem).FirstName = "New Name";
}

 

I am unable to get the above code to give the expected output...? 

However, there is more to my particular problem though -  I expect it may be that my object utilises iNotifyPropertyChanged - I think there maybe something occurring in that area, - see telerik.com/forums/raddataform-add-button-disabled

0
Matthew
Top achievements
Rank 1
answered on 28 Jul 2020, 10:59 AM

Vladimir - FYI, my code is not able get to the next event (AddedNewItem) when run, it is between these events that it fails with the NullException. 

 

Thanks

Matthew

0
Vladimir Stoyanov
Telerik team
answered on 31 Jul 2020, 06:36 AM

Hello Matthew,

Thank you for the update.

By setting the DataItem property of the event arguments to the CurrentItem you are forcing the same instance to be added twice in the RadDataForm, since the CurrentItem is already present in its ItemsSource. This is not recommended and that is why I suggested creating a new instance. You can also take a look at Examples 3 and 4 from the Adding New Entries article, which demonstrate that idea. Please, give the approach demonstrated in the article a try and let me know how it goes. 

Regards,
Vladimir Stoyanov
Progress Telerik

Tags
DataForm
Asked by
Matthew
Top achievements
Rank 1
Answers by
Vladimir Stoyanov
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or