How to Find DataFormItems

I'm trying to assign the values of my sfDataForm.  Here is my code:

Patient pat = new Patient();



if (this.patientDataForm.Items != null)


{
    foreach (var viewItem in this.patientDataForm.Items)
    {
        var tempCount = patientDataForm.Items;
        var item0 = patientDataForm.Items[0];
        var item1 = patientDataForm.Items[1];


        var item2 = patientDataForm.Items[2];


        if (viewItem is DataFormItem dataFormItem)
        {
            string fieldName = dataFormItem.FieldName;


            string labelText = dataFormItem.LabelText;


            var value = dataFormItem.GetValue();


            try
            {
                if (fieldName == "id")
                {
                    pat.id = Convert.ToInt16(dataFormItem.GetValue());
                }
                else if (fieldName == "lname")
                {
                    pat.lname = dataFormItem.GetValue().ToString();
                }
                else if (fieldName == "fname")
                {
                    pat.fname = dataFormItem.GetValue().ToString();
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }


            // var value = dataFormItem.GetValue();
        }
        else if (viewItem is DataFormGroupItem groupItem)


        {
            string groupName = groupItem.Name;


            var groupItems = groupItem.Items;
        }
    }
}

Here is my model with DataForm decorations




 public class Patient
 {
     [SQLite.AutoIncrement, SQLite.PrimaryKey]
     public int id { get; set; }


     [Display(GroupName = "Name")]
     [DataFormDisplayOptions(RowOrder = 0)]
     public string fname { get; set; }


     [Display(GroupName = "Name")]
     [DataFormDisplayOptions(RowOrder = 1)]
     public string lname { get; set; }
 }

I get an array exception error on the item2 assignment.

How do I get the values of the fields in the DataForm?




1 Reply

VM Vidyalakshmi Mani Syncfusion Team October 31, 2023 02:16 PM UTC

Hi Frederick,


Based on the shared code snippets, we have checked your code. According to the code, there are two items in the dataform: one is a DataformNumericItem, and the other is a DataformGroupItem. You have assigned the same group name to 2 editors, causing them to be grouped under a single DataformGroupItem. The array exception occurred when attempting to access the third item, which does not exist. To solve this, we suggest accessing all items in the dataform group item by first obtaining the DataFormGroupItem from Dataform.Items and then iterating through all the editors in the DataFormGroupItem. Please refer to the following code snippet for your reference.


Code snippet:

 

private void Button_Clicked(object sender, EventArgs e)

{

    Patient pat = new Patient();

 

    if (this.patientDataForm.Items != null)

    {       

        foreach (var viewItem in this.patientDataForm.Items)

        {          

            if (viewItem is DataFormItem dataFormItem)

            {

                 string fieldName = dataFormItem.FieldName;

                 string labelText = dataFormItem.LabelText;

                 var value = dataFormItem.GetValue();              

            }

 

            if (viewItem is DataFormGroupItem groupItem)

            {

                var groupItems = groupItem.Items;

                foreach (var item in groupItems)

                {

      string fieldName = item.FieldName;

      string labelText = item.LabelText;

                     var value = item.GetValue();                 

                }

            }

        }

    }

}

 



We hope that this helps you. Please let us know if you need any further assistance.



Loader.
Up arrow icon