Add default values to addnew record

i'm using CurrentRecordContextChange to check if use enter start entering new row and i have  a data set and relational table
i'm watching CurrentRecordContextChange and check if table name equal to parent and if table name equal to child to set the default data from the previous record 
i success with parent table 
with child table i got addnew not called and i'm sure i called it
this is my code

private static void Gv_CurrentRecordContextChange(object sender, CurrentRecordContextChangeEventArgs e)
        {
            if (e.Action == Syncfusion.Grouping.CurrentRecordAction.EnterRecordComplete &
                e.Record != null && e.Record.Kind == DisplayElementKind.AddNewRecord)
            {
                GridGroupingControl ggc = sender as GridGroupingControl;
                if (e.Table.TableDescriptor.Name == "Face")
                {
                    int currRowIndex = e.Table.CurrentElement.GetRowIndex();
                    Face lastFace = null;

                    for (int i = currRowIndex - 1; i >= 3; i--)
                    {
                        if (ggc.Table.DisplayElements[i].ParentRecord.GetData() is Face face)
                        {
                            lastFace = face;
                            break;
                        }
                    }

                    Face newFace = new Face();

                    if (lastFace == null)
                    {
                        newFace.ColumnNumber = 1;
                        newFace.RowNumber = 1;
                    }
                    else
                    {
                        newFace.RowNumber = lastFace.RowNumber;
                        newFace.ColumnNumber = lastFace.ColumnNumber + 1;
                    }

                    e.Table.AddNew();
                    foreach (PropertyInfo Propinfo in typeof(Face).GetProperties())
                    {
                        if (Propinfo.CanWrite)
                            e.Table.AddNewRecord.SetValue(Propinfo.Name, Propinfo.GetValue(newFace));
                    }
                }
                if (e.Table.TableDescriptor.Name == "DataTexts")
                {
                    int currRowIndex = e.Table.CurrentElement.GetRowIndex();

                    ClssDataText lastDataText = null;
                    for (int i = currRowIndex - 1; i >= 3; i--)
                    {
                        if (ggc.Table.DisplayElements[i].ParentRecord.GetData() is ClssDataText cdt)
                        {
                            lastDataText = cdt;
                            break;
                        }
                    }

                    ClssDataText newDataText = new ClssDataText();
                    if (lastDataText == null)
                    {
                        newDataText.ArrowDirection = ArrowDirectionsEnum.None;
                        newDataText.Order = 1;
                    }
                    else
                    {
                        newDataText.ArrowDirection = lastDataText.ArrowDirection;
                        newDataText.Order = lastDataText.Order + 1;
                    }

                    e.Table.AddNew();

                    foreach (PropertyInfo Propinfo in typeof(ClssDataText).GetProperties())
                    {
                        if (Propinfo.CanWrite)

                            e.Table.AddNewRecord.SetValue(Propinfo.Name, Propinfo.GetValue(newDataText)); // here i got 'AddNew not called'
                    }
                }
            }
        }

My model look like 

    public class Face
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public FaceDirectionsEnum FaceDirection { get; set; } = FaceDirectionsEnum.Forward;
        public bool IsElectronic { get; set; }
        public int RowNumber { get; set; } = 1;

        public int ColumnNumber { get; set; } = 1;
        public List DataTexts { get; set; }

        public Face()
        {
            DataTexts = new List();
        }
    }





10 Replies 1 reply marked as answer

BT Balamurugan Thirumalaikumar Syncfusion Team January 28, 2021 12:50 PM UTC

Hi KHALID, 

Thank you for using syncfusion products. 

We have checked your query "Add default values to addnew record" at our end. We have already documented about how to add a default value to add new record for tables by using ShowDefaultValuesInAddNewRecord property. 

You can refer the same from the following link. 

Please let us know if you would require further assistance. 

Regards, 
Balamurugan Thirumalaikumar

Marked as answer

KO KHALID OMAR MOHHMED January 31, 2021 12:38 AM UTC

thank you for replying but how can i set default value of enum type column becuase i got "invalid cast from string to enum"
and i want when use cancel adding new the value disappears 


BT Balamurugan Thirumalaikumar Syncfusion Team February 1, 2021 02:03 PM UTC

Hi KHALID, 

Thanks for the update. 

We have checked  the reported query at our end. Also we have checked our dashboard sample enum localization to check the reported issue. Unfortunately we could reproduce the reported issue at our end. 

Can you please check the attached sample and let us know that whether we missed anything to reproduce the issue at our end. So that, we could analyse further to provide you a better solution at the earliest. 
 
Regards, 
Balamurugan Thirumalaikumar  




KO KHALID OMAR MOHHMED replied to Balamurugan Thirumalaikumar February 6, 2021 11:49 AM UTC

Hi KHALID, 

Thanks for the update. 

We have checked  the reported query at our end. Also we have checked our dashboard sample enum localization to check the reported issue. Unfortunately we could reproduce the reported issue at our end. 

Can you please check the attached sample and let us know that whether we missed anything to reproduce the issue at our end. So that, we could analyse further to provide you a better solution at the earliest. 
 
Regards, 
Balamurugan Thirumalaikumar  



sorry the the sample you provided not alow add new and setting the default value i need how to set a default value of enum field that i set column description like that


            columns[0].Appearance.AnyRecordFieldCell.CellType = GridCellTypeName.ComboBox;
            columns[0].Appearance.AnyRecordFieldCell.DataSource = Helpers.GetArrowDirectionsEnumDisplayList();
            columns[0].Appearance.AnyRecordFieldCell.ValueMember = "item1";
            columns[0].Appearance.AnyRecordFieldCell.DisplayMember = "item2";

i think you should to allow to setting this prop (e.Table.TableDescriptor.Fields[Propinfo.Name].DefaultValue) to object not string


BT Balamurugan Thirumalaikumar Syncfusion Team February 9, 2021 03:55 AM UTC

Hi KHALID,

Thanks for the update.

We could understand the scenario of your last update. We are preparing a simple sample for default value of enum field. We will update you the details on or before February 09,2021. We appreciate your patience till then.

Regards
Balamurugan



BT Balamurugan Thirumalaikumar Syncfusion Team February 10, 2021 04:07 AM UTC

Hi KHALID, 

Thank you for your patience. 

In gridgrouping control this “gridGroupingControl1.TableDescriptor.Fields[column.Name].DefaultValue” property have implemented type of string. So we could not change this property type to object. We have prepared a simple sample to set default value of enum column and add new record. We have prepared the simple sample as per your requirement and you can download the same from the following location. Please let us know if you would require further assistance. 


Thanks & Regards, 
Balamurugan Thirumalaikumar  




KO KHALID OMAR MOHHMED February 18, 2021 10:33 AM UTC

thank you ,
i check your example and i found the behavior of data grid change when set the datasource to list and set to datatable , i modified the sample you provided to show the error when setting the datasource to list 

Attachment: Test_b99e1b5d.zip


BT Balamurugan Thirumalaikumar Syncfusion Team February 20, 2021 07:19 PM UTC

Hi KHALID, 

Thanks for the update. 

i check your example and i found the behavior of data grid change when set the datasource to list and set to datatable , i modified the sample you provided to show the error when setting the datasource to list 
We have checked this query, as per the current implementation of GridGrouping control’s DefaultValue property derived in string type. And also we can not change the DefaultValue In run time to the type of enum. 
i'm watching CurrentRecordContextChange and check if table name equal to parent and if table name equal to child to set the default data from the previous record  
i success with parent table  
with child table i got addnew not called and i'm sure i called it 
Meantime we could understand this first query and reproduce the issue with child table, we have forwarded this query to our development team for further validation will update you the proper details on February 23,2021. We appreciate your patience till then. 

Regards 
Balamurugan.Thirumalaikumar 



BT Balamurugan Thirumalaikumar Syncfusion Team February 23, 2021 02:11 PM UTC

Hi KHALID, 

Sorry for the inconvenience. 

We are facing too many difficulties to fix the reported issue in sample level. we have forwarded this issue to concern team for further validation. we will provide you the details on February 25,2021. we appreciate your patience till then. 

Regards, 
Balamurugan Thirumalaikumar  





AR Arulpriya Ramalingam Syncfusion Team February 26, 2021 04:01 AM UTC

Hi KHALID, 
 
Thank you for your patience. 
 
We have validated the reported scenario and as per the current behavior GridGroupingControl, the value of AddNewRecord could not be set manually for the child tables. In order to overcome this scenario, we suggest that you to set the DefaultValue property to load the current record value in the AddNewRecordRow and change it to empty once the current cell is moved. We have modified the sample and please make use of the below code example. 
 
Example code 
 
private void GridGroupingControl1_CurrentRecordContextChange(object sender, Syncfusion.Grouping.CurrentRecordContextChangeEventArgs e) 
{ 
    if (e.Action == Syncfusion.Grouping.CurrentRecordAction.EnterRecordComplete & 
        e.Record != null && e.Record.Kind == Syncfusion.Grouping.DisplayElementKind.AddNewRecord) 
    {                         
        //Code to update parent table record. 
         
        if (e.Table.TableDescriptor.Name == "DataTexts") 
        { 
            int currRowIndex = e.Table.CurrentElement.GetRowIndex(); 
 
            DataTexts lastDataText = null; 
            for (int i = currRowIndex - 1; i >= 3; i--) 
            { 
                if (ggc.Table.DisplayElements[i].ParentRecord.GetData() is DataTexts cdt) 
                { 
                    lastDataText = cdt; 
                    break; 
                } 
            } 
 
            DataTexts newDataText = new DataTexts(); 
            if (lastDataText == null) 
            { 
                newDataText.Direction = Position.Left; 
                newDataText.Order = 1; 
            } 
            else 
            { 
                newDataText.Direction = lastDataText.Direction; 
                newDataText.Order = lastDataText.Order + 1; 
            } 
 
            var table = gridGroupingControl1.GetTable("DataTexts"); 
            if (table != null) 
            { 
                table.AddNew(); 
                foreach (PropertyInfo Propinfo in typeof(DataTexts).GetProperties())  
                { 
                    if (Propinfo.CanWrite) 
                    { 
                        //table.AddNewRecord.SetValue(Propinfo.Name, Propinfo.GetValue(newDataText)); // here i got 'AddNew not called' 
                        table.TableDescriptor.Fields[Propinfo.Name].DefaultValue = Propinfo.GetValue(newDataText).ToString(); 
                    } 
                } 
            } 
        } 
    } 
 
    if (e.Action == Syncfusion.Grouping.CurrentRecordAction.LeaveRecordComplete & 
        e.Record != null && e.Record.Kind == Syncfusion.Grouping.DisplayElementKind.AddNewRecord) 
    { 
        if (e.Table.TableDescriptor.Name == "DataTexts") 
        { 
            var table = gridGroupingControl1.GetTable("DataTexts"); 
            if (table != null) 
            { 
                table.AddNew(); 
                foreach (PropertyInfo Propinfo in typeof(DataTexts).GetProperties()) 
                { 
                    if (Propinfo.CanWrite) 
                    { 
                        table.TableDescriptor.Fields[Propinfo.Name].DefaultValue = ""; 
                    } 
                } 
            } 
        } 
    } 
 
} 
 
Regards, 
Arulpriya R. 


Loader.
Up arrow icon