RowValidating event not fired after commit added row

Hi,

I use SfDataGrid to show a complex model,some simple property just binded directly,and there are some complex settings i use a popup window(by GridTemplateColumn) for them.

When i add a new row with simple property edited,commited and triggered RowValidating event successfully.
Then i click the "extend setting" button while adding the next new row,the row is commited,but RowValidating event  not fired.(and add the third row,the program is crashed)

How can i make it work

Thanks
The code is:

<Syncfusion:SfDataGrid ItemsSource="{Binding}"
                               AllowEditing="True"
                               AddNewRowPosition="Bottom"
                               RowValidating="sfDataGridTest_RowValidating"
                               LostKeyboardFocus="sfDataGridTest_LostKeyboardFocus">
            <Syncfusion:SfDataGrid.Columns>
                <Syncfusion:GridTextColumn MappingName="Name"/>


                <Syncfusion:GridTemplateColumn HeaderText="Extend Setting">
                    <Syncfusion:GridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Extend Setting" Click="Button_Click"/>
                        </DataTemplate>
                    </Syncfusion:GridTemplateColumn.CellTemplate>
                </Syncfusion:GridTemplateColumn>
            </Syncfusion:SfDataGrid.Columns>
        </Syncfusion:SfDataGrid>

 public partial class MainWindow : Window
    {
        private List<ViewModel> ViewModels { get; } = new List<ViewModel>();


        public MainWindow()
        {
            DataContext = ViewModels;
            InitializeComponent();
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window window = new Window();
            window.ShowDialog();
        }


        private void sfDataGridTest_RowValidating(object sender, RowValidatingEventArgs e)
        { 
            //checking user input and then commit or cancel
            MessageBox.Show("RowValidating");
        }


        private void sfDataGridTest_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (!(sender is SfDataGrid grid)) return;
            if (!grid.IsKeyboardFocusWithin)
            {
                if (grid.View != null && grid.View.CurrentEditItem != null && !grid.View.IsAddingNew)
                {
                    // it commit the modified value in the grid.
                    grid.View.CommitEdit();


                }
                if (grid.View != null && grid.View.IsAddingNew)
                {
                    // it commit the entered value in the grid.
                    if (grid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)
                        grid.SelectionController.CurrentCellManager.EndEdit(true);


                    var addNewRowController = grid.GetAddNewRowController();
                    addNewRowController.CommitAddNew();
                }
            }
        }
    }


    public class ViewModel
    {
        public string Name { get; set; }


        //other complex properties
    }



6 Replies

VS Vijayarasan Sivanandham Syncfusion Team July 20, 2022 03:30 PM UTC

Hi Wssyncfusion,

Please find answer for your queries below

Queries

Solutions

 

Then i click the "extend setting" button while adding the next new row,the row is commited,but RowValidating event  not fired.

 

 

Your requirement to trigger the RowValidating event in SfDataGrid can be achieved by setting focus to a particular element inside Template when the cell gets activated or edited. Please refer to the below code snippet,

<Syncfusion:GridTemplateColumn HeaderText="Extend Setting" MappingName="Customers.CustomerID" Syncfusion:FocusManagerHelper.WantsKeyInput="True" >

                    <Syncfusion:GridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <Button Content="Extend Setting" Syncfusion:VisualContainer.WantsMouseInput="True"  Syncfusion:FocusManagerHelper.FocusedElement="True" Click="Button_Click"/>

                        </DataTemplate>

                    </Syncfusion:GridTemplateColumn.CellTemplate>                 

</Syncfusion:GridTemplateColumn>

 

 

UG Link: https://help.syncfusion.com/wpf/datagrid/column-types#keyboard-interaction-for-uielement-loaded-inside-celltemplate

KB Link: https://www.syncfusion.com/kb/2526/how-to-focus-a-particular-uielement-inside-datatemplate-after-calling-currentcell-beginedit

 

https://www.syncfusion.com/kb/2433/how-to-handle-keyboard-and-mouse-interactions-for-gridtemplatecolumn

 

 

and add the third row,the program is crashed


The reported problem occurs due to the new row not being properly committed in SfDataGrid.

You can overcome this problem by committing the new row in a button click. Please refer to the below code snippet,

private void Button_Click(object sender, RoutedEventArgs e)

        {

            //the reported problme occurs due to not properly committed new row in SfDataGrid

            //Here propely commi the new row in SfDataGrid

            //still IsAddingNew enabled due to addnew row not properly committed

            if (sfDataGrid.View != null && sfDataGrid.View.IsAddingNew)

            {

                //here commi the new row properly when button clicking in GridTemplateColumn            

                var addNewRowController = sfDataGrid.GetAddNewRowController();

                addNewRowController.CommitAddNew();              

            }

 

            Window window = new Window();

            window.ShowDialog();

}



 KB Link: https://www.syncfusion.com/kb/5099/how-to-do-addnewrow-operations-programmatically-in-the-sfdatagrid

 


Please find the sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


Attachment: Sample_36e09a09.zip


WS wssyncfusion July 21, 2022 02:43 AM UTC

Hi,


Thansk for your reply,it solved almost all my problem,only the exception can still be raised following the steps :

1.Add a button or other control to the sample above

2.Click the CustomerName column of the new row adding a new record

3.Click the "Extend Setting" button once, RowValidating event fired and button click event doesn't

4.Click the new button or other control, LostKeyboardFocus event fired and CommitAddNew method excuted

5. Click the CustomerName column of the new row again,the exception raised


The reported problem occurs due to the new row not being properly committed in SfDataGrid.

You can overcome this problem by committing the new row in a button click. Please refer to the below code snippet,

I also add a handle to commit new row on new button control, but the exception still threw

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (sfDataGrid.View != null && sfDataGrid.View.IsAddingNew)
            {
                // it commit the entered value in the sfDataGrid.
                if (sfDataGrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)
                    sfDataGrid.SelectionController.CurrentCellManager.EndEdit(true);




                var addNewRowController = sfDataGrid.GetAddNewRowController();
                addNewRowController.CommitAddNew();
                Console.WriteLine("Button_Click_1 CommitAddNew");
            }
        }


Thanks



VS Vijayarasan Sivanandham Syncfusion Team July 21, 2022 03:52 PM UTC

Hi Wssyncfusion,

We have modified the sample based on the provided information. The reported issue is “Invalid exception occurs while clicking the Extend Setting button” and is unable to replicate the issue from our end. It is working fine as expected. Please find the tested sample and video in the attachment.


If we misunderstood your requirement, please provide more information related to your query?

  • Details about the replication procedure with a video illustration of the reported issue occur in the attached sample


Kindly revert to us with the above requested details. It will be more helpful for us to check the possibilities to resolve the reported problem.

Regards,

Vijayarasan S


Attachment: Sample_And_Video_Demo_6332661d.zip


WS wssyncfusion July 22, 2022 01:54 AM UTC

Thanks for replying,


The sample in video work perfectly,but when i run the code,the exception occurs again.


This is my enviroment:

Windows 10 21H2 119044.1415

Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.1.5


And i also took a video about the replication procedure


Thanks


Attachment: video_demo_a34b3e66.zip


WS wssyncfusion July 22, 2022 02:00 AM UTC

This is the compiled program in the video


Attachment: Debug_f4fd6245.zip


VS Vijayarasan Sivanandham Syncfusion Team July 22, 2022 03:17 PM UTC

Hi Wssyncfusion,

The reported problem occurs due to trying to edit another cell before committing the previous edited cell’s value. You can resolve the reported problem by committing the previous edited cell’s value like below mentioned code snippet,

private void sfDataGridTest_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)

{

            if (!(sender is SfDataGrid grid)) return;

            if (!grid.IsKeyboardFocusWithin)

            {

                if (grid.View != null && grid.View.CurrentEditItem != null && !grid.View.IsAddingNew )

                {

                    // it commit the modified value in the grid.

                    grid.View.CommitEdit();

                 

 

                }

                if (grid.View != null && grid.View.IsAddingNew)

                {

                    // it commit the entered value in the grid.

                    if (grid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)

                        grid.SelectionController.CurrentCellManager.EndEdit(true);

 

                    //need  to check the item is still editing or not

                    //if items is editing need to commit the value

                    if (grid.View.IsEditingItem)

                        grid.View.CommitEdit();

 

                    var addNewRowController = grid.GetAddNewRowController();

                    addNewRowController.CommitAddNew();

 

                }

            }

}


Please find the modified sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


Attachment: ModifiedSample_c288af03.zip

Loader.
Up arrow icon