Hi, in my code i must enter a check on the entered value during edit mode (like a custom validation). There is an Telerik.Grid event that triggers this moment where i can check the validation (UI must disable some controls in the page until "logical" validation is not ok, so user can insert an invalid value, but i want control that user choose one valid value before re-enable this controls).
Thanks, Massimiliano
7 Answers, 1 is accepted
The DataGrid control provides a Validation functionality out of the box. The control also provides ValidateCell command that you can use to handle the errors (if there are such) and make any desired visualization.
I have prepared a sample for you that demonstrates the validation functionality of the DataGrid - please, check it.
Hope this helps.
Regards,
Nasko
Progress Telerik
The DataGrid control does not provide an event, but provides a command that is invoked when editing is about to end.
So, in order to achieve the desired by you functionality you can create a custom CommitEdit command and write your validation there. Inside the command you can get the value of the cell your are currently editing and based on the validation logic either to invoke the command or to prevent finishing the edit operation until valid value is entered.
I have modified the sample sent in my previous response with the described above approach and you can give it a try.
I hope this will be helpful for you.
Regards,
Nasko
Progress Telerik
Thank Nasko, but i've other questions about your solution. CustomValidateCommand instance is starting on costructor of the page where i use the telerik grid. It's work fine, but i don't understand who call Execute Method (i asked you because i want set parameter to pass to Execute Method where i can set some object (page properties), that i can investigate when execute method is finished.
For Example, imagine that I have "Boolean" values on the page that establish the validity of the data present on it. Among these values there are some related to the data entered in the grid. for example, we hypothesize that I have a Boolean telling me (going back to the example I mentioned a few days ago) if the city I entered is in the list of "valid" cities. We will then have a property on the grid page called "IsValidCity". How can I return the real value of this property to the page from the Execute method? In the constructor I can of course pass different parameters (for example the list of valid cities). I can then assign a property to the CustomValidateCommad object which is evaluated on the basis of what is passed by the manufacturer and can therefore at that point in the Execute determine if the inserted city is "valid". How can I return this value back? Assuming that there are several lines, I would like to assign "False" to this property if in at least one of the lines there is an invalid city (if I understand correctly the execute I have the focus of the single line I am modifying). The alert can not be used because the page logic requires different behavior. A possible work around would be to use a pickerColumn, but I have another problem, since the list of valid external cities (which is part of another object), I tried to load the data inside an ObservableCollection, but at the moment I do not see the objects contained in the ItemSource on the picker column.
In my gridView i add this column :
<
telerikDataGrid:DataGridPickerColumn
PropertyName
=
"VatP"
CellContentFormat
=
"{}{0:N}"
HeaderText
=
"VAT P"
Width
=
"100"
SizeMode
=
"Fixed"
ItemsSource
=
"VatRate"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
TextFontSize
=
"10"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
FontSize
=
"12"
SelectedTextColor
=
"White"
HorizontalTextAlignment
=
"End"
>
</
telerikDataGrid:DataGridTextCellStyle
>
</
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
</
telerikDataGrid:DataGridPickerColumn
>
Where VatRate is a simple object with decimal property :
public
class
VatRate : ModelObject
{
public
decimal
vat;
public
decimal
VatP {
get
=> vat;
set
{ vat = value; RaisePropertyChanged(
"VatP"
); } }
public
VatRate()
{
}
}
And modelObject is this :
public
class
ModelObject : INotifyPropertyChanged
{
public
event
PropertyChangedEventHandler PropertyChanged;
protected
void
RaisePropertyChanged(
string
name)
{
if
(PropertyChanged !=
null
)
PropertyChanged(
this
,
new
PropertyChangedEventArgs(name));
}
}
And i've in .cs code beheind i create a LoadVatRateMethod :
private void LoadVateRateSource()
{
VatRateListTest = new ObservableCollection<
VatRate
>();
List<
decimal
> vatRateList = new List<
decimal
>() { 10, 5, 15, 22 };
foreach (var item in vatRateList )
{
VatRate myVat = new VatRate();
myVat.VatP = item;
VatRateListTest.Add(myVat);
}
}
where VatRateListTest is an object of VatRate type. When i edit row nothing is displayed in Vat P colum
The approach you have found with the event is really nice. Also, your observation about the validation command are absolutely right - it has been called twice.
The first time is called by the CanExecute method of the CommitEdit command and the second time from the Execute method of that same command. I have logged this issue in our feedback portal and on the following link you can track its status - if you subscribe to the item once its status gets changed you will be notified:
https://feedback.telerik.com/Project/168/Feedback/Details/250364-datagrid-validation-command-is-called-twice
As a workaround until the issue gets fixed I can only suggest to invoke your event when the Validation is invoked the second time - this is the time that gets called when the Execute method of the Commit command is validating the value. Here is a sample code you can use:
public
class
CustomValidateCommand : DataGridCommand
{
private
static
int
validationCount = 0;
public
CustomValidateCommand()
{
this
.Id = DataGridCommandId.ValidateCell;
}
public
override
void
Execute(
object
parameter)
{
if
(validationCount > 0)
{
validationCount = 0;
//Execute your logic here.
}
else
{
validationCount++;
}
}
}
I apologize for the inconvenience the issue might be causing you. I have updated your Telerik points for bringing this to our attention.
If you have any additional questions or concerns regarding Telerik controls, please let me know.
Regards,
Nasko
Progress Telerik