How to convert a DataRow to DataRowView or vice versa.

Hello,

I am trying to implement Data Validation so that empty cells or rows are marked as error and hence cannot be committed. The code I've written so far is:


        private void sfDataGrid1_RowValidating(object sender, Syncfusion.WinForms.DataGrid.Events.RowValidatingEventArgs e)
        {
            DataRowView dr = e.DataRow.RowData as DataRowView; //ERROR PRONE LINE [Breaks the code for AddNewRow]

            string[] ROWDAT = new string[dr.Row.ItemArray.Length];
            ROWDAT = Array.ConvertAll(dr.Row.ItemArray, Convert.ToString);
            foreach (string item in ROWDAT)
            {
                if (string.IsNullOrEmpty(item))
                {
                    e.ErrorMessage ="Records cannot be empty";
                    e.IsValid = false;
                    break;
                }
            }

            if (sfDataGrid1.SelectionController.DataGrid.IsAddNewRowIndex(e.DataRow.Index))
            {
                sfDataGrid1.SelectionController.DataGrid.CurrentCell.CancelEdit();
            }
        }

you can see the line commented as ERROR PRONE LINE and that is exactly where I find myself completely stuck. When the Row Type changes the code completely behaves opposite and abnormally.

When I add a new row and leaves some rows empty to test validation, it fails with the following error:

It says that the DataRowView dr was null which causes the error. However, When I try to edit EXISTING Rows in the View, the code runs fine without any exception.

During this scenario I was able to infer that the AddNewRow Row is not being casted to DataRowView causing the DataRowView object to hold null. 

But if I tweak the code a bit like:

        private void sfDataGrid1_RowValidating(object sender, Syncfusion.WinForms.DataGrid.Events.RowValidatingEventArgs e)
        {
            DataRow dr = e.DataRow.RowData as DataRow; //STILL ERROR PRONE [Breaks the Code for EXISTING Rows in the View]
            string[] ROWDAT = new string[dr.ItemArray.Length];
            ROWDAT = Array.ConvertAll(dr.ItemArray, Convert.ToString);
            foreach (string item in ROWDAT)
            {
                if (string.IsNullOrEmpty(item))
                {
                    e.ErrorMessage ="Records cannot be empty";
                    e.IsValid = false;
                    break;
                }
            }

            if (sfDataGrid1.SelectionController.DataGrid.IsAddNewRowIndex(e.DataRow.Index))
            {
                sfDataGrid1.SelectionController.DataGrid.CurrentCell.CancelEdit();
            }
        }


It starts working for AddNewRow Row but breaks the functionality for the EXISTING Rows in the view that are easily convertible to DataRowView but not to DataRow with the following error:



further, I found a useful information in Release Notes of Syncfusion for WindowsForms vesion V17.3.0.14 that type retrieved for inbuilt AddNewRow is DataRow and not DataRowView which I think is the Root cause of my problem.



What Is the way I can retrieve a common type for all AddNewRow Rows and Rows in View so the Validation works for both without any exception. Are they convertible? If whats the other way?

3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team June 16, 2021 07:28 AM UTC

Hi Shankul gupta,

Thank you for contacting Syncfusion Support.

Your requirement can be achieved by check the AddNewRow case and make customization in RowValidating event in SfDataGrid. Please refer the below code snippet, 
private void SfDataGrid1_RowValidating(object sender, Syncfusion.WinForms.DataGrid.Events.RowValidatingEventArgs e) 
{ 
            System.Data.DataRow dr = null; 
 
            //check the row is AddNewRow or Rows 
            if (sfDataGrid1.IsAddNewRowIndex(e.DataRow.RowIndex)) 
            { 
                //get the RowData details of AddNewRow 
                dr = e.DataRow.RowData as System.Data.DataRow; 
            } 
            else 
            { 
                //get the RowData details of Rows 
                DataRowView rowdata = e.DataRow.RowData as DataRowView;  
                dr = rowdata.Row; 
            } 
 
            string[] ROWDAT = new string[dr.ItemArray.Length]; 
            ROWDAT = Array.ConvertAll(dr.ItemArray, Convert.ToString); 
            foreach (string item in ROWDAT) 
            { 
                if (string.IsNullOrEmpty(item)) 
                { 
                    e.IsValid = false; 
                    e.ErrorMessage = "Records cannot be empty"; 
                    break; 
                } 
            } 
 
            if (sfDataGrid1.IsAddNewRowIndex(e.DataRow.Index)) 
            { 
                sfDataGrid1.CurrentCell.CancelEdit(); 
            } 
} 

For more information related to Data Validation, please refer the user guide documentation,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/datavalidation 

Please let us know if you have any concerns in this. 
 
Regards, 
Vijayarasan S 



SG shankul gupta June 19, 2021 04:53 AM UTC

Thank you for the reply, thats exactly what I was trying to achieve.

Edit: Currently when there is a invalid row the SfDataGrid prevents navigating through other rows or even leaving the SfDataGrid or even more closing the application.

So, How to make the grid reset to its original state (if a row is invalid) as soon as it loses focus or after validation is finished checking so that it doesn't halt the overall usability of the application.


VS Vijayarasan Sivanandham Syncfusion Team June 21, 2021 12:44 PM UTC

Hi Shankul gupta, 

Thanks for the update. 

SfDataGrid will not allow to edit other cell / row if validation failed. The current cell will not end the editing until the CurrentCell validation is passed. To get pass the cell validation need to enter the valid input to the cell. This is the behavior of SfDataGrid. 

For more information related to DataValidation, please refer the user guide documentation,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/datavalidation#custom-validations 
Based on provided information we suspect that in your SfDataGrid Row is invalid lost the focus. we have checked the reported problem and unable to replicate the issue from our end. it is working fine as expected. Please find the tested sample and video demo from our end in the below link,

Sample link: https://www.syncfusion.com/downloads/support/forum/166393/ze/Sample1962114826

 

Can you please share us below things?         
        1. Provide the replication procedure with video illustration of the reported issue 
        2. Could you please provide more details about your scenario with illustrations?  

if you still facing the same issue? If yes, please modify the sample based on your scenario.  

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

Regards, 
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon