Dynamic Fields in DataForms

Hi!

Can I create in DataForms with data coming from a table?
Example:
Label -> "Base" Field -> "COMBOBOX" -> items "Rigida;Flexible" EnumList
Label -> "Fabricante" Field -> "TEXT"
Label -> "Number" Field -> "NUMERIC"
Label -> "Revised" Field -> "BOOLEAN" -> items "Yes;No" 


Any an example of how to do it?


8 Replies 1 reply marked as answer

MS Muniappan Subramanian Syncfusion Team June 15, 2021 01:17 PM UTC

Hi Rodrogo,  
 
Thank you for using Syncfusion products. 
 
We checked your requirement “Can I create in DataForms with data coming from a table?”. we have achieved your requirement and fetch the data from the table. We’ve prepared the sample for the same. Please find the link below for the sample, 
 
Code Snippet: 
public void GenerateSource(int count) 
{ 
    dt = new DataTable("DataSheet"); 
    dt.Columns.Add("Base", typeof(string)); 
    dt.Columns.Add("Fabricante", typeof(string)); 
    dt.Columns.Add("Number", typeof(string)); 
    dt.Columns.Add("Revised", typeof(string)); 
 
    for (int i = 0; i < count; i++) 
    { 
        dt.Rows.Add("Rigida", "Fabricante Item", random.Next(0, 100), true); 
    } 
 
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
        var row = dt.Rows[i].ItemArray; 
        this.contactsInfo.Base = (row[i] as string); 
        this.contactsInfo.Fabricante = (row[i + 1] as string); 
        this.contactsInfo.Number = (row[i + 2] as string); 
 
        if ((row[i + 3] as string) == "True") 
        { 
            this.contactsInfo.Revised = true; 
        } 
        else 
        { 
            this.contactsInfo.Revised = false; 
        } 
    } 
} 
 
 
We hope that this helps you, kindly revert us if you have any concern.  
 
Regards, 
Muniappan S. 



UN Unknown Syncfusion Team June 16, 2021 08:30 PM UTC

Thank you for your help!
What do I need every row in my table to be a component(text, number, dropdown). How to make?
Searching the forum, I found something I modified. I don't know if this is correct.
How do I pass viewmodel data to DataFormItemManager

    public class DataFormItemManagerExt : DataFormItemManager
    {
        List dataFormDictionary;
        public DataFormItemManagerExt(SfDataForm dataForm, List dictionary) : base(dataForm)
        {
            dataFormDictionary = dictionary;
        }

        protected override List GenerateDataFormItems(PropertyInfoCollection itemProperties, List dataFormItems)
        {
            var items = new List();

            foreach (var key in dataFormDictionary)
            {
                DataFormItem dataFormItem;
                switch (key.FieldType)
                {
                    case FieldTypeEnum.Number:
                        dataFormItem = new DataFormNumericItem() { Order = key.FieldID, LabelText = key.NameData, Name = key.ValueData, Editor = "Numeric", MaximumNumberDecimalDigits = 0 };
                        break;
                    case FieldTypeEnum.Boolean:
                        dataFormItem = new DataFormDropDownItem() { Order = key.FieldID, LabelText = key.NameData, Name = key.ValueData, Editor = "DropDown" };
                        List listBoolean = new List();
                        listBoolean.Add(AppResources.No);
                        listBoolean.Add(AppResources.Yes);
                        (dataFormItem as DataFormDropDownItem).ItemsSource = listBoolean;
                        break;
                    case FieldTypeEnum.Date:
                        dataFormItem = new DataFormDateItem() { Order = key.FieldID, LabelText = key.NameData, Name = key.ValueData, Editor = "Date" };
                        break;
                    case FieldTypeEnum.Enum:
                        dataFormItem = new DataFormDropDownItem() { Order = key.FieldID, LabelText = key.NameData, Name = key.ValueData, Editor = "DropDown" };

                        string[] parts = key.EnumList.Split(';');
                        List listEnum = new List(parts);
                        (dataFormItem as DataFormDropDownItem).ItemsSource = listEnum;

                        break;
                    case FieldTypeEnum.Text:
                    case FieldTypeEnum.Image:
                    default:
                        dataFormItem = new DataFormItem() { Order = key.FieldID, LabelText = key.NameData, Name = key.ValueData, Editor = "Text" };
                        break;
                }

                items.Add(dataFormItem);
            }
            return items;
        }
}

I applied the example to my project. This error is occurring even with ContactsInfo with values
Java.Lang.IllegalArgumentException: 'Cannot add a null child view to a ViewGroup'
What will it be?


MS Muniappan Subramanian Syncfusion Team June 17, 2021 11:05 AM UTC

Hi Rodrogo,  
 
Thank you for using Syncfusion products. 
 
We checked your requirement “Create dataform items based on the Dictionary”. You can achieve your requirement by loading the dataform with custom dictionary by generating DataFormItems manually. To create DataFormItems from dictionary, override the GenerateDataFormItems method. We’ve prepared the sample for the same. Please find the link below for the sample, 
 
Code Snippet: 
// dataform item creating using dictionary. 
dataForm.DataObject = new object(); 
dataFormViewModel = bindable.BindingContext as DataFormViewModel; 
dataForm.ItemManager = new DataFormItemManagerExt(dataForm, dataFormViewModel.dictionary); 
 
public class DataFormItemManagerExt : DataFormItemManager 
{ 
    Dictionary<string, object> dataFormDictionary; 
    public DataFormItemManagerExt(SfDataForm dataForm, Dictionary<string, object> dictionary) : base(dataForm) 
    { 
        dataFormDictionary = dictionary; 
    } 
 
    protected override List<DataFormItemBase> GenerateDataFormItems(PropertyInfoCollection itemProperties, List<DataFormItemBase> dataFormItems) 
    { 
        var items = new List<DataFormItemBase>(); 
        foreach (var key in dataFormDictionary.Keys) 
        { 
            DataFormItem dataFormItem; 
            if (key == "Base") 
            { 
                dataFormItem = new DataFormDropDownItem() { Name = key, Editor = "DropDown" }; 
                List<string> baseSource = new List<string>(); 
                baseSource.Add("Rigida"); 
                baseSource.Add("Flexible"); 
                (dataFormItem as DataFormDropDownItem).ItemsSource = baseSource; 
            } 
            else if (key == "Fabricante") 
            { 
                dataFormItem = new DataFormTextItem() { Name = key, Editor = "Text" }; 
            } 
            else if (key == "Number") 
            { 
                dataFormItem = new DataFormNumericItem() { Name = key, Editor = "Numeric", MaximumNumberDecimalDigits = 0 }; 
            } 
            else if (key == "Revised") 
            { 
                dataFormItem = new DataFormDropDownItem() { Name = key, Editor = "DropDown" }; 
                List<string> baseSource = new List<string>(); 
                baseSource.Add("true"); 
                baseSource.Add("false"); 
                (dataFormItem as DataFormDropDownItem).ItemsSource = baseSource; 
            } 
            else 
                dataFormItem = new DataFormTextItem() { Name = key, Editor = "Text" }; 
            items.Add(dataFormItem); 
        } 
        return items; 
    } 
} 
 
 
Please find the output for the same, 
 
 
Please refer the below link for more details about generating dataform items manually in SfDataForm, 
 
We hope that this helps you, kindly revert us if you have any concern.  
 
Regards, 
Muniappan S. 


Marked as answer

UN Unknown Syncfusion Team June 17, 2021 07:49 PM UTC

Hi! Thanks.

I think this helps.
I have problem OnAttachedTo is executed before loading my data(BindingContext is null).
Should I use another method?

 

It wouldn't be correct, but I inverted the code and have data access.

        public MachineDetailPage(ListTreeview comp)
        {
            BindingContext = new EquipmentViewModel(comp);
            InitializeComponent();
        }

Now there is data, but error occurs:
System.InvalidCastException: 'Specified cast is not valid.' in TEXT type only. DropDown, Numeric e Date is OK.

I added it to xaml and the problem with the TEXT type continues
Anything about the problem?

ERROR with DataFormTextItem



UN Unknown Syncfusion Team June 18, 2021 12:28 PM UTC

Hi!

Solved the problem with TEXT.
I added this.

LayoutOptions="TextInputLayout"


MS Muniappan Subramanian Syncfusion Team June 18, 2021 01:17 PM UTC

Hi Rodrigo,  
 
Thank you for the update. 
 
Query : I have problem OnAttachedTo is executed before loading my data (BindingContext is null). 
We have checked your reported query. By setting the BindingContext of ContentPage in XAML, you can get the viewmodel data from behavior class.  Please refer the below code snippet to set BindingContext in xaml. 
 
Code Snippet: 
 
<ContentPage.BindingContext> 
    <dataformxamarin:DataFormViewModel x:Name="ViewModel"/> 
</ContentPage.BindingContext> 
     
<ContentPage.Behaviors> 
    <dataformxamarin:DataFormBehavior/> 
</ContentPage.Behaviors> 
 
<ContentPage.Content> 
    <Grid Padding="0,20,0,0"> 
        <dataForm:SfDataForm x:Name="dataForm" AutoGenerateItems="False"> 
            <dataForm:SfDataForm.Items> 
                <dataForm:DataFormTextItem Name="Name" Editor="Text"/> 
                <dataForm:DataFormMaskedEditTextItem Name="Phone" Editor="MaskedEditText"/> 
                <dataForm:DataFormTextItem Name="Address" Editor="MultilineText"/> 
                <dataForm:DataFormDateItem Name="BirthTime" Editor="Date"/> 
            </dataForm:SfDataForm.Items> 
        </dataForm:SfDataForm> 
    </Grid> 
</ContentPage.Content> 
 
 
Query 2: ERROR with DataFormTextItem 
We are glad that the reported issue has been resolved at your side. Please get in touch with us if you require any further assistance. As always we are happy to help you out. 
 
We hope that this helps you, kindly revert us if you have any concern.  
 
Regards,  
Muniappan S 



UN Unknown Syncfusion Team June 18, 2021 06:56 PM UTC

Thank you for your help!

Just to finish, if it's a bug
LayoutOptions="Default" error occurs.

Regards,
Rodrigo


MS Muniappan Subramanian Syncfusion Team June 21, 2021 12:18 PM UTC

Hi Rodrigo,  
 
Thank you for the update. 
 
Query: ERROR with DataFormTextItem in Default Layout 
We have checked the reported issue and it’s working fine from our end. As per the instruction we have followed to check the reported issue and we are unable to replicate the issue from our end. We have prepared sample based on the given code snippets and attached the tested sample in the following link,  
 
 
Additional information:  
Tested device: Moto G5 and iPhone simulator 
SfDataForm version: 19.1.0.69 
Xamarin Forms: 4.5.0.617 
 
Please check our sample and let us know if you still facing the same issue? if not, please modify our sample and revert us back with the following details which would be helpful for us to check on it and provide you the solution as soon as possible, 
 
·         Share the issue replicate video.  
·         Share the issue reproducible sample  
·         Share Dataform code snippets  
·         Share Syncfusion and Xamarin.Form’s version 
 
Regards,  
Muniappan S 


Loader.
Up arrow icon