This is a migrated thread and some comments may be shown as answers.

Anyway to force row/cell validation

11 Answers 1393 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lasse
Top achievements
Rank 1
Lasse asked on 23 Jan 2012, 10:12 AM
Hi,

Is there anyway to force row/cell validation from outside the grid?
Is there anyway to force data back to the data-set before validation?

Regards
Svein Thomas

11 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 26 Jan 2012, 09:49 AM
Hi Svein Thomas,

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).

0
Bob
Top achievements
Rank 2
answered on 21 Jun 2012, 01:30 AM
I think I am looking for the same effect.

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
0
Bob
Top achievements
Rank 2
answered on 21 Jun 2012, 01:42 AM
I placed a BeginEdit / EndEdit around the cell update.
However in my for each loop, the last row is not being updated...I'll look a little deeper.
Thanks
0
Bob
Top achievements
Rank 2
answered on 21 Jun 2012, 01:53 AM
OK - sorry for the drawn out process.

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
0
Jack
Telerik team
answered on 25 Jun 2012, 10:37 AM
Hello Bob,

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
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Bob
Top achievements
Rank 2
answered on 25 Jun 2012, 03:16 PM
Thank you for the reply.

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
0
Jack
Telerik team
answered on 28 Jun 2012, 07:39 AM
Hi 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
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Bob
Top achievements
Rank 2
answered on 28 Jun 2012, 02:38 PM
Hi Jack,

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
0
Jack
Telerik team
answered on 03 Jul 2012, 08:58 AM
Hello 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
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 16 Mar 2020, 12:58 AM

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

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Mar 2020, 01:26 PM

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Lasse
Top achievements
Rank 1
Answers by
Jack
Telerik team
Bob
Top achievements
Rank 2
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or