NewRow ErrorText

1 Answer 14 Views
GridView
Hell
Top achievements
Rank 1
Iron
Iron
Hell asked on 23 Jan 2025, 03:03 PM | edited on 23 Jan 2025, 03:35 PM

I still haven't found a solution to this problem: https://www.telerik.com/forums/cellvalidating-event-question

asked

1. When user inputs an invalid value for a cell, we can use ErrorText to show an error message in the RowHeaderColumn. Normally, the ErrorText will be cleared after the user corrects the value. Sometimes the user will just press Esc key to cancel the input value, in this case the CellValidating event won't get fired and the ErrorText will remain shown on the RowHeaderColumn. This behavior will make the user feel confused.

answered

1. CellValidating event occurs only when a cell value is changed. In case when Esc is pressed, the value remains the same and no validation is needed. However, you can handle CellEndEdit event in this case and reset the ErrorText property.


Private Sub grdURLs2_UserAddingRow(sender As Object, e As GridViewRowCancelEventArgs) Handles grdURLs2.UserAddingRow
    Try
        If TypeOf e.Rows(0) Is GridViewNewRowInfo AndAlso
        TryCast(e.Rows(0).Cells(0).ColumnInfo, GridViewDataColumn) IsNot Nothing AndAlso
           TryCast(e.Rows(0).Cells(1).ColumnInfo, GridViewDataColumn) IsNot Nothing Then
            If e.Rows(0).Cells(0).Value Is Nothing OrElse
            String.IsNullOrEmpty(Trim(e.Rows(0).Cells(0).Value)) Then
                e.Cancel = True
                DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = "Empty!"
            ElseIf e.Rows(0).Cells(1).Value Is Nothing OrElse
                String.IsNullOrEmpty(Trim(e.Rows(0).Cells(1).Value)) Then
                e.Cancel = True
                DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = "Empty!"
            ElseIf e.Rows(0).Cells(1).Value IsNot Nothing Then
                For Each row In grdURLs2.Rows
                    If e.Rows(0).Cells(1).Value = row.Cells(1).Value Then
                        e.Cancel = True
                        DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = "Exists!"
                    End If
                Next
            End If
        End If
    Catch ex As Exception
        txtError.Text = ex.Message & " - " & Format(Now, "HH:mm:ss")
    End Try
End Sub

Private Sub grdURLs2_CellEndEdit(sender As Object, e As GridViewCellEventArgs) Handles grdURLs2.CellEndEdit
    Try
        If TypeOf e.Row Is GridViewNewRowInfo Then
            DirectCast(e.Row, GridViewNewRowInfo).ErrorText = String.Empty
        End If
    Catch ex As Exception
        txtError.Text = ex.Message & " - " & Format(Now, "HH:mm:ss")
    End Try
End Sub

This doesn't work. The CellEndEdit event does not fire.


1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 24 Jan 2025, 08:40 PM

Hello, Hell,

It seems you have found an answer in the referred thread to the question that you need. I reviewed it, and confirm this solution is applicable for the case that you describe. CellValidating event occurs only when a cell value is changed. In case when Esc is pressed, the value remains the same, and no validation is needed. However, you can handle CellEndEdit event in this case and reset the ErrorText property. The CellEndEdit fires every time when a cell exit edit mode, either by using the Esc key or clicking somewhere outside the cell. When the editing process tops, CellEndEdit triggers.

I put a breakpoint in the CellEndEdit event, and then after pressing the Esc key at run time, the project hits the breakpoint and the logic there can be executed. It is strange if the event does not trigger on your side. Do you have something different in your setup? I am sharing to you my sample test project. You can refer to it and test how it works. If you have further difficulties, feel free to modify my project and demonstrate there the problem that you have.

Offtopic, I noticed that you are using a Trial version of our Telerik UI for WinForms controls. For the period of using the trial version, you are eligible to submit a ticket from your Telerik account. In case you have an urgent question, I would recommend you to submit a ticket because tickets are handled before forum threads. Tickets are handled by support representatives and you can expect faster replies from us, while the forum channel is mostly intended to be used by the community. I am sharing this information with you in case you decide to benefit from the trial. If you decide to submit your ticket next time, the following KB article will guide you how to do that: How to Get the Most Out of the Telerik UI for WinForms Support - Telerik UI for WinForms

I hope the shared information is sufficient for you. In case you need any further assistance, do not hesitate to contact me.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Hell
Top achievements
Rank 1
Iron
Iron
commented on 24 Jan 2025, 09:51 PM | edited

Yes. I'm using the trial version to learn and understand. And I'm not in a hurry. And no. I still haven't found an answer to my question. Your example is missing error handling. For example:

Private Sub RadGridView1_UserAddingRow(sender As Object, e As GridViewRowCancelEventArgs) Handles RadGridView1.UserAddingRow
    If TypeOf e.Rows(0) Is GridViewNewRowInfo Then
        If e.Rows(0).Cells(1).Value Is Nothing Then
            e.Cancel = True
            DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = "Empty!"
        Else
            DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = ""
        End If
    End If
End Sub

If there was error handling and e.cancel, then you would understand what I'm talking about. The CellEndEdit event is triggered only on the first Esc when editing of a cell is finished (then the cell value is checked and the ErrorText value is assigned to Error and e.cancel to true). On the second ESC adding a new record is canceled and the CellEndEdit event naturally does not trigger, but the Error value in ErrorText remains. Nobody resets it. How can I fix this? Thanks!

Hell
Top achievements
Rank 1
Iron
Iron
commented on 24 Jan 2025, 10:38 PM | edited

I am attaching my project with an error (your project slightly corrected) and a video with an error for you.

Nadya | Tech Support Engineer
Telerik team
commented on 29 Jan 2025, 08:52 AM

Hello, Hell,

The behavior that you observe is normal since the GridViewNewRowInfo is still in edit mode when you are trying to close the form and it does not close. To prevent this, you should call the CancelAddNewRow(). It will clear the whole row (all the values in cells). Please refer to the following code snippet:

 Private Sub RadGridView1_UserAddingRow(sender As Object, e As GridViewRowCancelEventArgs) Handles RadGridView1.UserAddingRow
     If TypeOf e.Rows(0) Is GridViewNewRowInfo Then
         If e.Rows(0).Cells(1).Value Is Nothing Then
             'e.Cancel = True
             DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = "Empty!"
             Me.RadGridView1.MasterView.TableAddNewRow.CancelAddNewRow()
         Else
             DirectCast(e.Rows(0), GridViewNewRowInfo).ErrorText = ""
         End If
     End If
 End Sub

In case, you may need to preserve the already entered values and do no lose them, you can create a custom new row and introduce the desired behavior there in a custom way. You can take a look at the following forum thread: https://www.telerik.com/forums/event-useraddingrow#4493549 

I hope this is useful. Can you give these suggestions a try and let me know are they suitable for you.

Hell
Top achievements
Rank 1
Iron
Iron
commented on 02 Feb 2025, 10:21 PM | edited

Where did I ask you about closing the form? I asked about ErrorText. Where do you see that GridViewNewRowInfo is in edit mode? But ErrorText remains.

Your code with CancelAddNewRow() also doesn't work.

Do you even read what they ask you? Do you download user projects? Are you checking?

Nadya | Tech Support Engineer
Telerik team
commented on 05 Feb 2025, 02:18 PM

Hello,

Yes, I have downloaded the attached WindowsApp2 project. I am confused that the code I suggested did not work. Now, I will provide the updated project so you can download it and test it directly. Also, I can see from one of your videos that after editing you are trying to close the form, but it does not close immediately as the row editing is not complete.

CellEndEdit event triggers every time when the cell ends editing.

I watched carefully the videos you submitted and tried to follow the same steps on my end. Below I am writing down the steps that I follow. If I am missing something, please correct me.

1. Enter the first cell from the new row in column 1 in edit mode.

2. The icon changes to pencil, indicating that the row has been editing at this moment:

3. Type 5.

4. Press Esc key to cancel the editing.

5. The cell exit edit mode. CellEndEdit triggers. As result, the icon now shows arrow, indicating this is the current row.

6. Press Esc second time and the "Click here to add a new row" text appears again.

I am also providing a short video:

I also tested the same project with entering value in the second column, or leaving it empty, the error text appeared as defined (only if first cell remains empty). Am I missing something? Please let me know if you are still having difficulties achieving your specific behavior. It would be great if you can specify the exact steps that you follow, in case they differ from mine or if there are additional steps to reproduce.

In order to provide a proper solution for a specific case it necessary first to understand the exact problem, and what is the desired behavior.

I am looking forward to your reply.

 

Tags
GridView
Asked by
Hell
Top achievements
Rank 1
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or