11 Answers, 1 is accepted
Thank you for contacting us. Directly to your questions:
1. CellValidating and RowValidating events fire when the current cell/row is being changed or when ending an edit operation. You can force these events to fire by setting the CurrentRow/CurrentColumn properties to different value or by calling the EndEdit method to finish the current edit operation.
2. No values are changed in the data source before validation. However, I am not sure that I fully understand your scenario. Please, could you elaborate a bit more and describe in detail what you want to achieve. I will be glad to assist further.
I am looking forward to your reply.
Regards,
Jack
the Telerik team
SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).
I have a grid with values in it; quantity, price, discount (%), unit price.
If the user wished to place a discount on each row, they enter it once, and then in a For Each loop, I place the value in the cell; however the calculation does not take place. it appears as if the CellValidating or RowValidating events are not firing.
Here is my snipit of code:
For
Each
r
As
GridViewDataRowInfo
In
InvoiceDetailsGridView.Rows
r.Cells(
"colDiscount"
).Value = DiscountTextBox.Value
Next
InvoiceDetailsGridView.Refresh()
The user can manually enter a value in the discount field, and the calculation takes place.
Any ideas?
Thanks
Bob
However in my for each loop, the last row is not being updated...I'll look a little deeper.
Thanks
When I go through the loop, the last row is not displaying the calculated value, although the CellEndEdit event is fired performing the calculation and placing the values in the right cell. When I run through the loop once more, the same thing happens; the last row does not update with the current value, but displays the results of what should have been there from the previous calculation.
Hope that makes sense.
Any ideas?
Thanks
Could you please send us your application and describe in detail the exact behavior that you want to achieve? This will help us to understand the exact scenario and to provide you with a working solution. Please note that CellValidating event fires only when changing the cell value using the user interface. This method will not fire when setting the cell value with code.
I am looking forward to your reply.
Greetings,
Jack
the Telerik team
Ok, to make sure I understand, when I update a field through code, the calculations will not take place due to the CellValidating event not firing. If that is the case, is there a way to fire that event or do I have to manually calculate the values and manually update each field?
I need to break out the portion of code and will get that shortly; just wanted to follow up with the question above.
Thanks
Bob
Yes, your assumption is correct. In this case the CellValidating event will not fire. I am not sure whether it helps, however you can force this event to fire by using the EventDispatcher class. Here is a sample:
GridViewRowInfo row =
this
.radGridView1.Rows[1];
GridViewColumn column =
this
.radGridView1.Columns[
"Value"
];
object
oldValue = row.Cells[
"Value"
].Value;
object
newValue = 10;
CellValidatingEventArgs cellValidating =
new
CellValidatingEventArgs(row, column, newValue, oldValue,
null
);
this
.radGridView1.MasterTemplate.EventDispatcher.RaiseEvent<CellValidatingEventArgs>(EventDispatcher.CellValidating,
this
.radGridView1, cellValidating);
if
(!cellValidating.Cancel)
{
row.Cells[
"Value"
].Value = newValue;
}
If you need further assistance, I will be glad to help.
All the best,
Jack
the Telerik team
Thanks for the information.
I ended up using the CurrentRow.InvalidateRow() method in the CellEndEdit event of the the grid to perform the calculations.
Interesting that it works, just not sure I understand how.
Thanks for the help; you guys are great!
Bob
I am glad to hear that you have found a solution for this issue.
The InvalidateRow method synchronizes the visual row element with the row content in the data layer. So, this seems to be a potential issue. Please, could you send me your application and describe the exact steps to reproduce it. We will investigate it and if there is an issue, we will try to address it in our upcoming service pack.
Thank you in advance.
I am looking forward to your reply.
Greetings,
Jack
the Telerik team
Just wanted to say thanks for the code snippet of how to trigger the validate event.
In my case, I was loading data that possibly contained errors ( when opening data into an editing form).The user must update these..
I wanted a way to have cell validation for when the user tried changing values.. but also needed to handle the user hits 'Save' without even giving the grid focus. ( so no validate events are fired)
So I just made this generic sub to fire off a validate event for every cell and then check the whole grid had no errors. I am loading a small amount of data, so works well.
Private Function IsValid(Grid As RadGridView) As Boolean
For r = 0 To Grid.Rows.Count -1
For c As Integer = 0 to Grid.Columns.Count -1
Dim row As GridViewRowInfo = Grid.Rows(r)
Dim column As GridViewColumn = Grid.Columns(c)
If column.IsVisible Then
Dim Value As Object = Grid.Rows(0).Cells(c).value
Dim cellValidating As CellValidatingEventArgs = New CellValidatingEventArgs(row, column, Value, Value, Nothing)
Grid.MasterTemplate.EventDispatcher.RaiseEvent(Of CellValidatingEventArgs)(EventDispatcher.CellValidating,Grid, cellValidating)
End if
Next
Next
'Now to look through the row's checking for any errors (Validate event handle adds to the row Errortext)
For re = 0 To Grid.Rows.Count -1
if NOT String.IsNullOrWhiteSpace(CustomerGrid.Rows(re).ErrorText) Then Return false
next
return true
End Function
Maybe helps someone
Rob
Hello, Rob,
Thank you for sharing your code with the community.
I have updated your Telerik points for community effort.
Note that you can also iterate the cells for all rows in RadGridView and simply check the value. It is not actually necessary to fire the CellValidating event in order to properly validate the cells.
However, feel free to use this approach which suits your requirements best.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik