Cancel new row from ICollectionViewAdv

Hi,

I'm trying to cancel adding new row from MVVM, if 1 of the property is not valid.
XAML:
                                    EnableDataVirtualization="True"
                                    Grid.Row="1"
                                    Margin="5"
                                    AllowFiltering="True"
                                    AutoGenerateColumns="False"
                                    ColumnSizer="Star"
                                    EditTrigger="OnDoubleTap"
                                    AddNewRowPosition="FixedTop"
                                    ItemsSource="{Binding EquST}"
                                    BindableView="{Binding STView,Mode=TwoWay}"
                                    SelectedItem="{Binding Selected}"
                                    NavigationMode="Cell">
                                   
                                       
...

C# ViewModel:
public ICollectionViewAdv STView
        {
            get => _STView;
            set
            {
                _STView = value;
                if (_STView != null)
                {
                    _STView.CollectionChanged += View_CollectionChanged;
                }
            }
        }
 private void View_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                List item = e.NewItems.Cast().Select(x=>x.Data).Cast().ToList();
                    EquST touse = item.FirstOrDefault();
                    if (string.IsNullOrEmpty(touse.Model))
                    {
                        var asdsa=(GridQueryableCollectionViewWrapper)sender;
                        asdsa.CancelNew();
                    }
            }
        }

My goal is, to remove/cancel the new row, if the "Model" is not selected.

Regards,
Attila

7 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team March 19, 2021 01:32 PM UTC

Hi Attila Toroczkai,

Thank you for contacting Syncfusion Support.

Based on provided information we suspect that you are added the new row using AddNewRow support in SfDataGrid. In this case, one of the properties contains null or empty data the new row not added into SfDataGrid. Your requirement can be achieved by customize the RowValidating event in SfDataGrid. Please refer the below code snippet,
 
private void datagrid_RowValidating(object sender, RowValidatingEventArgs e) 
{ 
            if (this.datagrid.IsAddNewIndex(e.RowIndex)) 
            { 
                var data = e.RowData as Model; 
 
                //check the property  
                if (string.IsNullOrEmpty(data.CusId)) 
                {                     
                    if (this.datagrid.View.IsAddingNew) 
                    { 
                        //Which end edit the current cell. By passing false, it revert the entered value. 
 
                        if (this.datagrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing) 
                            this.datagrid.SelectionController.CurrentCellManager.EndEdit(true); 
 
                        var addNewRowController = this.datagrid.GetAddNewRowController(); 
                        //cancel the new row 
                        addNewRowController.CancelAddNew(); 
                    } 
                }                
            } 
} 

Sample Link: https://www.syncfusion.com/downloads/support/forum/163631/ze/Sample1014603626

You can validate the data in AddNewRow like other data rows through built-in validation or custom validation. Here, AddNewRow is validated using RowValidating event by setting RowValidatingEventArgs.IsValid to false which doesn’t allow users to commit the AddNewRow until the validation gets succeeded. For more information, please refer the below UG link,

UG Link:
https://help.syncfusion.com/wpf/datagrid/data-manipulation#validating-addnewrow

Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 


Marked as answer

AT Attila Toroczkai March 22, 2021 06:44 AM UTC

Hi Vijayarasan,

Thank you for your reply, but i think I wasn't good at describing my question.
I did the RowValidation and its OK when they edit the Grid, but I want to cancel/remove the new item that is added to the collection, with the AddNewRow in ViewModel.
I have more then 1 Grid on the window, so I would like to keep it's .cs file simple, only 1 line of code : DataContext = new ViewModel(); 
I edited your sample and attached it, now the change event will cast back to the Model, but the CancelNew() will not cancel the addition.
Currently thinking on adding EvenTrigger/EventCommand for RowValidating or catching the new row and removing it from the collection, in the change event.
If you have a simpler way or a sample for my current ideas, please let me know.

Regards,
Attila

Attachment: Sample_d43185df.zip


VS Vijayarasan Sivanandham Syncfusion Team March 23, 2021 05:08 PM UTC

Hi Attila Toroczkai,

Thanks for the update.

We are able to understand your reported scenario. We need some information related to your requirement.

Can you please share below things,
         1. Can you please confirm new row added using AddNewRow or code behind in SfDataGrid  
         2. Provide the details of adding new row process in SfDataGrid


It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S
 



AT Attila Toroczkai March 24, 2021 08:58 AM UTC

Hi Vijayarasan,

Thank you for your reply.
For clarification:
- SfDatagrid AddNewRow is set to fixed top and used to add the new row (no code behind, user should input the new data)
SfDataGrid has no RowValidation event, only using built-in validation
- When user finished the input and SfDataGrid adds the new row to the list, ICollectionViewAdv will trigger collection changed and that's where I would like to validate and cancel.

In my last reply, I attached the modified sample, that works similarly. Breaking in the if add statement, only the newly added row will be captured.

Regards,
Attila


VS Vijayarasan Sivanandham Syncfusion Team March 25, 2021 06:55 PM UTC

Hi Attila Toroczkai,

Thanks for the update.

Based on provided information while new row adding in DataGrid. Collection changed event trigger only after the record added into collection. In this case, we remove the row in CollectionChanged event does not reflect into collection still contains the newly added record in collection. This is the behavior of collection.  
So, we need to cancel the new row before adding into collection. As we mentioned earlier, you can achieve this by customization the RowValidating event in SfDataGrid.

UG Link: https://help.syncfusion.com/wpf/datagrid/data-manipulation#validating-addnewrow 
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S
 



AT Attila Toroczkai March 26, 2021 09:55 AM UTC

Hi Vijayarasan,

Thank you for your update.

I will go with the removing it from the collection, that I was thinking.

Thank you for your support.

Regards,
Attila


VS Vijayarasan Sivanandham Syncfusion Team March 26, 2021 03:50 PM UTC

Hi Attila Toroczkai,

 
Thanks for the update. 
 
Please let us know if you have any further queries on this. We are happy to help you😊. 
 
Regards, 
Vijayarasan S 


Loader.
Up arrow icon