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?