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

How to cancel the OnAddNewRowInitiating event

Hi, I noticed when using the new row functionality that it is quite easy to add a few blank rows, so I wanted to check in the OnAddNewRowInitiating if you need to finish other rows before creating the new one.

                <sf:SfDataGrid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="FieldsGrid" AutoGenerateColumns="False" ItemsSource="{Binding Path=Fields}"
                               AllowEditing="True" AllowSorting="False" AllowFiltering="False" AddNewRowPosition="Bottom"
                               EditTrigger="OnTap" CurrentCellActivating="FieldsGrid_OnCurrentCellActivating" AddNewRowInitiating="FieldsGrid_OnAddNewRowInitiating">

I can set the defaults no problem:

        private void FieldsGrid_OnAddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs args)
        {
            var newRow = new TableEditorFieldData
            {
                DataType = "Text",
                DataSize = 50
            };
            args.NewObject = newRow;           
        }

But I can't see an args.Cancel, do I just need to always delete the row manually if I do validation on other rows (and assume that the new row is always the last row as there is no args.Row property)?

thanks


3 Replies

SV Srinivasan Vasu Syncfusion Team May 18, 2016 02:05 PM UTC

Hi Craig, 
 
Thanks for contacting Syncfusion support. 
 
Query 1[Cancel AddNewRow ] 
 
 
We have analyzed your query and it is not possible to cancel AddNew operation in AddNewRowInitiating event. Instead of AddNewRowInitiating event you can use SfDataGrid.CurrentCellBeginEdit event like below code example. 
 
C# 
 
this.datagrid.CurrentCellBeginEdit += Datagrid_CurrentCellBeginEdit;     
 
    private void Datagrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs args) 
        { 
            if (this.datagrid.IsAddNewIndex(args.RowColumnIndex.RowIndex)) 
            { 
                args.Cancel = true;  
            }  
        } 
 
Sample Location: Samples 
 
 
Query2[Remove Row after Validation] 
We can’t understand your requirement clearly and Could you please share more details regarding your exact your requirement?  
 
 
 
 
 
Regards,
Srinivasan 
 



CG Craig Greenway May 18, 2016 05:46 PM UTC

Hi, I am trying to avoid the following scenario where you can create lots of blank rows:


The CurrentCellBeginEdit event didn't help, but using OnRowValidating ended up working well with Add New Row so I am happy for this to be considered closed :)

        private void FieldsGrid_OnRowValidating(object sender, RowValidatingEventArgs args)
        {
            var data = args.RowData.GetType().GetProperty("FieldName").GetValue(args.RowData);
            if (data == null || data.ToString().Equals(""))
            {
                args.IsValid = false;
                args.ErrorMessages.Add("FieldName", "Field Name is compulsory");
            }

            data = args.RowData.GetType().GetProperty("DataType").GetValue(args.RowData);
            if (data == null || data.ToString().Equals(""))
            {
                args.IsValid = false;
                args.ErrorMessages.Add("DataType", "Data Type is compulsory");
            }
            //other fields are given default values and cannot be blanked out, so no need to check on other fields
        }



SV Srinivasan Vasu Syncfusion Team May 19, 2016 04:47 AM UTC

Hi Craig, 
  
Thanks for your update. 
  
Regards, 
Srinivasan 


Loader.
Live Chat Icon For mobile
Up arrow icon