private void DataGrid_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs args) { if (args.PreviousRowColumnIndex.RowIndex <= 0) return; var record = this.dataGrid.GetRecordAtRowIndex(args.PreviousRowColumnIndex.RowIndex); var column =this.dataGrid.Columns[this.dataGrid.ResolveToGridVisibleColumnIndex(args.PreviousRowColumnIndex.ColumnIndex)]; if (column.MappingName.Equals("Salary")) { var cellValue = this.dataGrid.View.GetPropertyAccessProvider().GetValue(record, column.MappingName); if (cellValue != null && int.Parse(cellValue.ToString()) <= 0) { args.Cancel = true; MessageBox.Show("Salary should not be negative"); this.dataGrid.SelectionController.CurrentCellManager.BeginEdit(); } } } private void DataGrid_RowValidating(object sender, RowValidatingEventArgs args) { if (args.RowIndex <= 0) return; var record = this.dataGrid.GetRecordAtRowIndex(args.RowIndex); var column = this.dataGrid.Columns[this.dataGrid.ResolveToGridVisibleColumnIndex(4)]; if (column.MappingName.Equals("Salary")) { var cellValue = this.dataGrid.View.GetPropertyAccessProvider().GetValue(record, column.MappingName); if (cellValue != null && int.Parse(cellValue.ToString()) <= 0) { args.IsValid = false; MessageBox.Show("Salary should not be negative"); this.dataGrid.SelectionController.CurrentCellManager.BeginEdit(); } } } |