We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Get the Fields from a .NET Maui DataForm

I'm try to access the fields in a DataForm object.  

// Assuming you have already created a SfDataForm control and set its ItemsSource
SfDataForm dataForm = new SfDataForm();
dataForm.ItemsSource = yourDataSource;

// Get the fields and properties
var fields = dataForm.Fields;
foreach (var field in fields)
{
    var propertyName = field.Name; // Get the name of the property bound to the field
    var label = field.LabelText; // Get the label of the field
    var editorType = field.EditorType; // Get the editor type of the field
    // You can access other properties of the field as well
}


I don't see a "Fields" property or a DataItemObject



5 Replies

KA Karthikraja Arumugam Syncfusion Team May 1, 2023 10:08 AM UTC

Hi Frederick,

The DataForm generates data fields based on the provided DataObject. Please refer to the following documentation in the User Guide to learn how to provide a DataObject to the DataForm: https://help.syncfusion.com/maui/dataform/getting-started#creating-data-object


Once the DataObject is set and the view is loaded, we can access the data fields using the Items property of the DataForm. These Items are generated based on the given DataObject and are only available after the view is loaded.


Code snippet:

this.Loaded += this.OnMainPageLoaded;

   

    private void OnMainPageLoaded(object sender, EventArgs e)

    {

        if (this.dataForm.Items != null)

        {

            foreach (var viewItem in this.dataForm.Items)

            {

                if (viewItem is DataFormItem dataFormItem)

                {

                    string fieldName = dataFormItem.FieldName;

                    string labelText = dataFormItem.LabelText;

                }

                else if (viewItem is DataFormGroupItem groupItem)

                {

                    string groupName = groupItem.Name;

                    var groupItems = groupItem.Items;

                }

            }

        }

    }

 



Alternatively, you can set the Items property directly by disabling auto-generate fields in the DataForm. Please refer to the following documentation in the User Guide to learn about explicit items generation:

https://help.syncfusion.com/maui/dataform/dataform-settings#explicitly-create-data-editors


Regards,

Karthik Raja A



FS Frederick Switzer May 1, 2023 01:42 PM UTC

Karthik Raja A,

Thank you for that reply, but I'm trying to get the value/text for the field, for example if you have the following field in the data object. items:


 if (viewItem is DataFormItem dataFormItem)

                {

                    string fieldName = dataFormItem.FieldName;

                    string labelText = dataFormItem.LabelText;

                }


How would I find out what is the value in the text field, something like ?

var itemValue = dataFormItem.?????


Thanks, 

FTs






SS SaiGanesh Sakthivel Syncfusion Team May 2, 2023 12:15 PM UTC

Hi Frederick,


Your requirement can achieve with the help of ValidateProperty event. Inside the event, you can get the editor value. Please refer to the following code snippet for your reference.


Code snippet

private void dataForm_ValidateProperty(object sender, DataFormValidatePropertyEventArgs e)

{

    var value = e.NewValue;

}


Please refer to the demo sample in the attachment. Please let us know if you have any concerns.


Regards,
SaiGanesh Sakthivel


Attachment: DataFormMAUI_52aead56.zip


FS Frederick Switzer May 2, 2023 02:11 PM UTC

SaiGanesh Sakthivel,


Thank you , that is helpful.  However, I'm looking for a solution that will loop through all of the fields of a dataform and provide the e.value and e.propertyname.   

This would be useful if you want to save all of the fields to a database as part of a CRUD process. 


(Items below is just an example, I do not know if it is the correct terminology)

foreach(var items in dataform.Items){

console.Writeline(e.value.tostring()+"  "+e.propertyname.tostring());

}





SS SaiGanesh Sakthivel Syncfusion Team May 3, 2023 11:25 AM UTC

Hi Frederick,


Your requirement can achieve with the help of GetValue method for DataFormItem in Items class. Please refer to the following code snippet for your reference.


Code snippet

private void Button_Clicked(object sender, EventArgs e)

{

    //// - get the value of the editor

    if (this.dataForm.Items != null)

    {

        foreach (var viewItem in this.dataForm.Items)

        {

            if (viewItem is DataFormItem dataFormItem)

            {

                var value = dataFormItem.GetValue();

            }

        }

    }

}


Please refer to the demo sample in the attachment. Please let us know if you have any concerns.


Regards,
SaiGanesh Sakthivel


Attachment: DataFormMAUI_82293c65.zip

Loader.
Live Chat Icon For mobile
Up arrow icon