Example #1
0
        internal bool RaiseCellValidate(RowColumnIndex currentCellIndex, IGridCellRenderer renderer, bool allowattributeValidation)
        {
            if (IsCurrentCellValidated)
            {
                return(true);
            }

#if WinRT || UNIVERSAL
            // In winrt and universal , the datagrid is from click notification. where we need to call parent validation to raise the cell validation for child grid.
            //In Multiple SelectionMode, the SelectedDetailsViewDataGrid will be null when navigating through CurrentCell, hence we have added the condition by checking CurrentCell.
            if (dataGrid is DetailsViewDataGrid && !dataGrid.SelectionController.CurrentCellManager.HasCurrentCell)
            {
                var parentDataGrid = dataGrid.GetParentDataGrid();
                if (parentDataGrid.SelectedDetailsViewGrid == null || (parentDataGrid.SelectedDetailsViewGrid != null && dataGrid != parentDataGrid.SelectedDetailsViewGrid))
                {
                    return(parentDataGrid.ValidationHelper.RaiseCellValidate(parentDataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex, null, true));
                }
            }
#endif
            if (dataGrid.IsInDetailsViewIndex(currentCellIndex.RowIndex))
            {
                if (dataGrid.SelectionController is GridBaseSelectionController)
                {
                    var currentDetailsViewGrid = (dataGrid.SelectionController as GridBaseSelectionController).GetCurrentDetailsViewGrid(dataGrid);
                    return(currentDetailsViewGrid.ValidationHelper.RaiseCellValidate(currentDetailsViewGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex, null, true));
                }
            }
            // WPF-34211 - Need to avoid the row taken from RowGenerator items when the current row index is -1.
            var dataRow = dataGrid.RowGenerator.Items.FirstOrDefault(item => item.RowIndex == currentCellIndex.RowIndex && currentCellIndex.RowIndex != -1);
            if (dataRow == null)
            {
                return(false);
            }
            // WPF-34211 - Need to avoid the column taken from VisibleColumns when the current column index is -1.
            var columnBase = dataRow.VisibleColumns.FirstOrDefault(x => x.ColumnIndex == currentCellIndex.ColumnIndex && currentCellIndex.ColumnIndex != -1);
            if (columnBase == null)
            {
                return(false);
            }
            renderer = renderer ?? columnBase.Renderer;

            if (renderer == null)
            {
                return(false);
            }
            if (IsFocusSetBack)
            {
                return(false);
            }

            //WPF-36608 - CurrenctCellvalidating event are not fired, when we select cell on treeGrid template column.
            if (!columnBase.Renderer.CanValidate())
            {
                SetCurrentCellValidated(true);
                return(true);
            }
            object oldValue = null;
            object changedNewValue;
            string errorMessage;
            if (this.dataGrid.SelectionController is GridBaseSelectionController)
            {
                oldValue = (this.dataGrid.SelectionController as GridBaseSelectionController).CurrentCellManager.oldCellValue;
            }
            var newCellValue = renderer.GetControlValue();
            if (this.RaiseCurrentCellValidatingEvent(oldValue, newCellValue, columnBase.GridColumn, out changedNewValue, currentCellIndex, columnBase.ColumnElement as FrameworkElement, out errorMessage, dataRow.RowData))
            {
                bool isValid = true;
                if (newCellValue != changedNewValue)
                {
                    renderer.SetControlValue(changedNewValue);
                }
                if (allowattributeValidation && this.dataGrid.MergedCellManager.CanRasieEvent)
                {
                    if ((columnBase.ColumnElement is GridCell) && (columnBase.ColumnElement as GridCell).Content != null && (columnBase.ColumnElement as GridCell).Content is FrameworkElement)
                    {
                        renderer.UpdateSource((columnBase.ColumnElement as GridCell).Content as FrameworkElement);
                    }
                    if (columnBase.GridColumn.GridValidationMode != GridValidationMode.None)
                    {
                        isValid = this.dataGrid.ValidationHelper.ValidateColumn(dataRow.RowData, columnBase.GridColumn.MappingName, (columnBase.ColumnElement as GridCell), currentCellIndex) &&
                                  DataValidation.Validate((columnBase.ColumnElement as GridCell), columnBase.GridColumn.MappingName, columnBase.ColumnElement.DataContext);
                    }
#if !WinRT && !UNIVERSAL
                    if (!isValid && columnBase.GridColumn.GridValidationMode == GridValidationMode.InEdit)
                    {
                        return(false);
                    }
#endif
                }
                if (this.dataGrid.MergedCellManager.CanRasieEvent)
                {
                    this.RaiseCurrentCellValidatedEvent(oldValue, columnBase.Renderer.GetControlValue(), columnBase.GridColumn, errorMessage, dataRow.RowData);
                }
                SetCurrentCellValidated(true);
                return(true);
            }
            return(false);
        }
Example #2
0
        internal bool ValidateColumn(object rowData, string columnName, GridCell currentCell, RowColumnIndex currentCellIndex)
        {
            var  propertyName     = columnName;
            bool isValid          = true;
            var  errorMessage     = string.Empty;
            bool isAttributeError = false;

            if (rowData == null || string.IsNullOrEmpty(columnName) || currentCell == null || currentCellIndex == RowColumnIndex.Empty)
            {
                return(isValid);
            }
            var itemproperties = this.dataGrid.View.GetItemProperties();

            if (itemproperties == null)
            {
                return(isValid);
            }

            // WPF-25016 Using PropertyDescriptorExtensions for WPF and PropertyInfoExtensions for WinRT, the codes are cleaned up
            if (columnName.Contains('.'))
            {
                var propNames = columnName.Split('.');
                columnName = propNames[propNames.Length - 1];
                Array.Resize(ref propNames, propNames.Length - 1);
                var pName = string.Join(".", propNames);
#if WPF
                rowData = PropertyDescriptorExtensions.GetValue(itemproperties, rowData, pName);
#else
                rowData = Syncfusion.Data.PropertyInfoExtensions.GetValue(itemproperties, rowData, pName);
#endif
            }

            PropertyInfo      propertyinfo      = null;
            ValidationContext validationContext = null;

#if !WinRT
            if (rowData != null)
            {
                propertyinfo      = rowData.GetType().GetProperty(columnName);
                validationContext = new ValidationContext(rowData, null, null)
                {
                    MemberName = columnName
                };
            }
#else
            if (rowData != null)
            {
                propertyinfo      = rowData.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name == columnName);
                validationContext = new ValidationContext(rowData)
                {
                    MemberName = columnName
                };
            }
#endif

            if (errorMessages != null && rowIndex == currentCellIndex.RowIndex && errorMessages.Keys.Contains(columnName) && IsCurrentRowValidated)
            {
                errorMessage = errorMessages[columnName];
            }
            if ((this.dataGrid.Columns[propertyName] as GridColumn).GridValidationMode != GridValidationMode.None)
            {
                if (propertyinfo != null)
                {
                    var validationAttribute = propertyinfo.GetCustomAttributes(false).OfType <ValidationAttribute>();
                    var value   = propertyinfo.GetValue(rowData, null);
                    var results = new List <System.ComponentModel.DataAnnotations.ValidationResult>();
                    try
                    {
                        if (!Validator.TryValidateValue(value, validationContext, results, validationAttribute))
                        {
                            foreach (var result in results)
                            {
                                errorMessage = !string.IsNullOrEmpty(errorMessage) ? errorMessage + string.Format("\n") + result.ErrorMessage : errorMessage + result.ErrorMessage;
                            }
                            isValid          = false;
                            isAttributeError = true;
                        }
                    }
                    catch (Exception e)
                    {
                        errorMessage     = e.Message;
                        isValid          = false;
                        isAttributeError = true;
                    }
                }
            }

            if (currentCell != null)
            {
                if (!isValid || !string.IsNullOrEmpty(errorMessage))
                {
                    currentCell.SetCellValidationError(errorMessage, isAttributeError);
                }
                else
                {
                    currentCell.RemoveCellValidationError(true);
                }
            }
#if !SyncfusionFramework4_0 && !SyncfusionFramework3_5 || UWP
            if (rowData is INotifyDataErrorInfo)
            {
                isValid = isValid && DataValidation.ValidateINotifyDataErrorInfo(currentCell, columnName, rowData);
            }
#endif
            return(isValid);
        }
Example #3
0
        internal void ApplyRowHeaderVisualState()
        {
            if (this.DataGrid != null && !this.DataGrid.showRowHeader)
            {
                return;
            }
            var columnBase = this.VisibleColumns.FirstOrDefault(col => col.ColumnElement is GridRowHeaderCell);

            if (columnBase == null)
            {
                return;
            }
            GridRowHeaderCell rowHeaderCell = columnBase.ColumnElement as GridRowHeaderCell;

            if (rowHeaderCell == null)
            {
                return;
            }
            if (this.IsCurrentRow)
            {
                rowHeaderCell.State = "CurrentRow";
            }
            else if (this.RowType == RowType.AddNewRow)
            {
                rowHeaderCell.State = "AddNewRow";
            }
            else if (this.RowType == RowType.FilterRow)
            {
                rowHeaderCell.State = "FilterRow";
            }
            else
            {
                rowHeaderCell.State = "Normal";
            }

            if (this.IsEditing)
            {
                rowHeaderCell.State = "EditingRow";
            }

            var dataValidation = this.RowData;

            if (dataValidation != null)
            {
#if !SyncfusionFramework4_0 && !SyncfusionFramework3_5 || UWP
                if ((dataValidation as INotifyDataErrorInfo) != null)
                {
                    if (DataValidation.ValidateRowINotifyDataErrorInfo(this.RowData))
                    {
                        rowHeaderCell.RowErrorMessage = GridResourceWrapper.RowErrorMessage;
                        if (!rowHeaderCell.State.Equals("Normal"))
                        {
                            rowHeaderCell.State = "Error_CurrentRow";
                        }
                        else
                        {
                            rowHeaderCell.State = "Error";
                        }
                        rowHeaderCell.ApplyVisualState();
                        return;
                    }
                    else
                    {
                        if ((this.DataGrid.View.CurrentItem == null && (RowType == RowType.AddNewRow && !this.IsEditing)))
                        {
                            rowHeaderCell.State = "Normal";
                        }
                    }
                }
#endif
#if WPF
                if ((dataValidation as IDataErrorInfo) != null)
                {
                    if (DataValidation.ValidateRow(this.RowData))
                    {
                        rowHeaderCell.RowErrorMessage = (dataValidation as IDataErrorInfo).Error;
                        if (!rowHeaderCell.State.Equals("Normal"))
                        {
                            rowHeaderCell.State = "Error_CurrentRow";
                        }
                        else
                        {
                            rowHeaderCell.State = "Error";
                        }
                        rowHeaderCell.ApplyVisualState();
                        return;
                    }
                    else
                    {
                        if ((this.DataGrid.View != null && this.DataGrid.View.CurrentItem == null) && this.RowType != RowType.AddNewRow)
                        {
                            rowHeaderCell.State = "Normal";
                        }
                    }
                }
#endif
            }
            if (this.RowType == Grid.RowType.TableSummaryRow || this.RowType == Grid.RowType.TableSummaryCoveredRow)
            {
                rowHeaderCell.State          = "Normal";
                rowHeaderCell.GridCellRegion = "TableSummaryCell";
            }
            rowHeaderCell.ApplyVisualState();
        }