Questions : Need to perform CRUD operations using SfDataGrid.

Hello Team,

I need help on creating following scenarios with SfDataGrid. I want user to be able to perform CRUD operations using SfDataGrid with validation.

Question 1: When new row is added, which event is getting triggered which updates the UI ?
Question 2: Need to validate data entered by user, as I used TextBlock as cell template and TextBox as editing template.
Question 3: If data entered by user is correct in a row, when user hits enter button to move the curser to next new row with editing mode.

NOTE : I am using MVVM with framework : Caliburn.Micro

Thank you,
Dhairya Joshi

3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team May 20, 2021 05:07 PM UTC

Hi Dhairya Joshi,

Thank you for contacting Syncfusion Support.

Please find answer for your queries below 
Queries 
Solutions 
 
When new row is added, which event is getting triggered which updates the UI ? 
 
 
 
In SfDataGrid new row added or deleted SfDataGrid.View.Records.CollectionChanged event triggered. Please refer the below code snippet, 
sfDataGrid.Loaded += SfDataGrid_Loaded; 
 
private void SfDataGrid_Loaded(object sender, RoutedEventArgs e) 
{ 
            sfDataGrid.View.Records.CollectionChanged += Records_CollectionChanged; 
} 
 
private void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
            MessageBox.Show("Record Added"); 
}    
 
 
 

Need to validate data entered by user, as I used TextBlock as cell template and TextBox as editing template.
 
 
 
Your requirement can be achieved by using RowValidating event in SfDataGrid. Please refer the below code snippet,

 
private void sfDataGrid_RowValidating(object sender, RowValidatingEventArgs e) 
{ 
            var data = e.RowData.GetType().GetProperty("CustomerID").GetValue(e.RowData); 
 
            if (data != null && data.ToString().Equals("AROUT")) 
            { 
                e.IsValid = false; 
                e.ErrorMessages.Add("CustomerID", "Customer AROUT cannot be passed"); 
            }             
} 
 
Please refer the below user guide documentation,

UG Link: https://help.syncfusion.com/wpf/datagrid/data-validation#row-validation
 
 
 
If data entered by user is correct in a row, when user hits enter button to move the curser to next new row with editing mode. 
 
 
 
Your requirement can be achieved by call the BeginEdit method in SfDataGrid. Please refer the below code snippet,

 
private void sfDataGrid_CurrentCellEndEdit(object sender, CurrentCellEndEditEventArgs e) 
{            
            if (e.RowColumnIndex.ColumnIndex == this.sfDataGrid.GetLastColumnIndex()) 
            { 
                RowColumnIndex rowColumnIndex = new RowColumnIndex(e.RowColumnIndex.RowIndex + 1, this.sfDataGrid.GetFirstColumnIndex()); 
                this.sfDataGrid.MoveCurrentCell(rowColumnIndex); 
                this.sfDataGrid.SelectionController.CurrentCellManager.BeginEdit(); 
            } 
} 

Please refer the below user guide documentation,

UG Link: https://help.syncfusion.com/wpf/datagrid/editing#beginedit
 
 
 
Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Marked as answer

DJ Dhairya Joshi May 24, 2021 04:55 AM UTC

Thank you team,
you can close this.


VS Vijayarasan Sivanandham Syncfusion Team May 25, 2021 06:24 AM UTC

Hi Dhairya Joshi, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. 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