BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
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
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
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
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
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());
}
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