Converting a DataFormItem to a picker at run-time

Hi

I am trying to convert a DataFormTextItem into a DataFormPickerItem following this sample from the help page:

dataForm.AutoGeneratingDataFormItem += DataForm_AutoGeneratingDataFormItem;

private void DataForm_AutoGeneratingDataFormItem(object sender, AutoGeneratingDataFormItemEventArgs e)
{
    if (e.DataFormItem != null && e.DataFormItem.Name == "Name")
    {
        var list = new List<string>();
        list.Add("Home");
        list.Add("Food");
        list.Add("Utilities");
        list.Add("Education");
        (e.DataFormItem as DataFormDropDownItem).ItemsSource = list;
    }
}


However, I get a Null exception when I assign a list in the ItemsSource probably because the item is not a pickerItem on AutoGeneratingDataFormItem. Is it possible to create a list at run-time and convert the DataFormItem into a picker and assign the list?

This is the snipped code:

 detailsDataForm.AutoGeneratingDataFormItem += DetailsForm_AutoGenerateItem;

private void DetailsForm_AutoGenerateItem(object sender, AutoGeneratingDataFormItemEventArgs e)
        {
             if (e.DataFormItem != null && e.DataFormItem.Name == "Title")
            {            
                    var list = new List();
                    list.Add("Single");
                    list.Add("Married");
                    list.Add("Co-Habiting");
                    list.Add("Divorced/Separated");
                    list.Add("Widowed");
                    list.Add("Civil Partnership");
                    list.Add("Unspecified");

                    e.DataFormItem = new DataFormPickerItem()
                    {
                        Name = "Title",
                        Editor = "Picker",
                        ItemsSource = list,
                    }; 
                }          
        }


Thank you




4 Replies

JN Jayaleshwari N Syncfusion Team September 24, 2018 03:53 AM UTC

Hi Dave,   
    
Thank you for contacting Syncfusion support.   
    
Regarding query: convert a DataFormTextItem into a DataFormPickerItem    
    
By default, the ItemsSource for picker is auto-generated for enum types and in order to display the picker other than enum data type, you can either set the ItemSource of property type in SourceProvider of SfDataForm which is used to add custom ItemsSource of DataFormPickerItem or you can set ItemSource through AutoGeneratingDataFormItemevent in SfDataForm. In either way you need to register the editor using RegisterEditor as Picker/DropDown in order to make data form item as DataFormPickerItem/DataFormDropDownItem based on the xamarin forms platform used.   
    
Since Picker editor will not be supported in Xamarin.Forms.UWP platform and you can register the data form editor as DropDown for list, but it will not be supported in Xamarin.Forms iOS platform. You can refer our online documentation to know more about DropDownEditor.   
    
    
Sample: DataForm-Sample
  
   
Code snippet:   
    
    
            dataForm.DataObject = new ContactInfo();

            if (Device.RuntimePlatform != Device.UWP)
                dataForm.RegisterEditor("LastName", "Picker");
            else
                dataForm.RegisterEditor("LastName", "DropDown");  
 
    
private void DataForm_AutoGeneratingDataFormItem(object sender, AutoGeneratingDataFormItemEventArgs e)
        {
            if (e.DataFormItem != null && e.DataFormItem.Name == "LastName")
            {
                var list = new List<string>();
                list.Add("Home");
                list.Add("Food");
                list.Add("Utilities");
                list.Add("Education");

                if (Device.RuntimePlatform != Device.UWP)
                    (e.DataFormItem as DataFormPickerItem).ItemsSource = list;
                else
                    (e.DataFormItem as DataFormDropDownItem).ItemsSource = list;

            }
        }  
 
    
    
Though we had an issue with generating picker items through AutoGeneratingDataFormItem event. We have already found and fixed this issue in our Volume 2 SP2 release 2018. You can refer our release notes as well.   
    
    
Could you please check with the provided sample and if further issue is reproduced, kindly revert us back by modifying the sample with further details about your query along with the below detail,   
    
# SfDataForm vesion used.   
    
 Code snippet for DataObject class:   
    

    
public class ContactInfo 
    {
        private string firstName;
        private string lastName;
        private string title;

   
        [Display(Name = "First Name")]
        public string FirstName
        {
            get { return this.firstName; }
            set
            {
                this.firstName = value;
            }
        }

        [Display(Name = "Last Name")]
        public string LastName
        {
            get { return this.lastName; }
            set
            {
                this.lastName = value;
            }
        }

        public string Title
        {
            get { return title; }
            set
            {
                title = value;
            }
        }
    }  
 
    
    
    
Regarding query: create a list at run-time and convert the DataFormItem into a picker and assign the list?   
  
Yes it is possible to create a list at run time and convert the DataFormIten in to picker item in SfDataForm Xamarin.Forms. It can be achieved by using RefreshLayout method of SfDataForm which is used to refresh the data form item created. You need to pass the True in RefreshLayout method to regenerate all the items from the committed data object . Also you can refresh the particular data form item .Please refer the below link more details on RefreshLayout.  
  
  
Currently we have issue related to this query in our side. We have logged issue report for the same and we will include this fix in our upcoming Volume 3 SP1 release which is expected to be available by end of month October 30,2018 else kindly let us know if you need patch in advance.  
 
Regards, 
Jayaleshwari N 



DA Dave September 24, 2018 07:41 AM UTC

Thank you very much for your explanation and snipped code.


JN Jayaleshwari N Syncfusion Team September 25, 2018 05:33 AM UTC

Hi Dave, 
 
Thanks for the update. Please get in touch if you would require further assistance. 

Regards, 
Jayaleshwari N.


JN Jayaleshwari N Syncfusion Team December 11, 2018 07:21 AM UTC

Hi Dave,   
   
We are glad to announce that our Essential Studio 2018 Volume 4 beta Release v16.4.0.40 is rolled out and is available for download under the following link.    
    
     
The reported issue “Changing DataFormItem to DataFormPickerItem in AutoGeneratingDataFormItem causes NRE exception” has been resolved and included in this release .We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance.    
    
Regards,    
Jayaleshwari N. 


Loader.
Up arrow icon