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.
Unfortunately, activation email could not send to your email. Please try again.

How to cancel the OnAddNewRowInitiating event

Thread ID:

Created:

Updated:

Platform:

Replies:

124099 May 17,2016 02:08 AM May 19,2016 12:47 AM WPF 3
loading
Tags: SfDataGrid
Craig Greenway
Asked On May 17, 2016 02:08 AM

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


Srinivasan Vasu [Syncfusion]
Replied On May 18, 2016 10:05 AM

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 
 


Craig Greenway
Replied On May 18, 2016 01:46 PM

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
        }


Srinivasan Vasu [Syncfusion]
Replied On May 19, 2016 12:47 AM

Hi Craig, 
  
Thanks for your update. 
  
Regards, 
Srinivasan 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;