James
Posted 3 hours ago
I am attempting to add validation to my grid which has 3 columns: ID, NAME, STATUS
My data is not bounded to an adaptor (requirement).
I am trying to validate for the existence of name in my database on the RowsChanging event.
On NotifyCollectionChangedAction.Add , I pass the value of NAME to my stored procedure and determine if the NAME value exists; no problems.
On NotifyCollectionChangedAction.ItemChanging, I pass the values of ID and NAME to my stored procedure and determine if the NAME value exists.
Problem is, the NAME value passed on ItemChanging is what is currently in the cell, not what is in the editor. How can I get the editor value at this time?
12 Answers, 1 is accepted
Thank you for writing.
The GridViewCollectionChangingEventArgs object should contain both values at this point. I have tested this with the following code and I am getting the correct results:
Private Sub RadGridView1_RowsChanging(ByVal sender As Object, ByVal e As GridViewCollectionChangingEventArgs) If e.Action = NotifyCollectionChangedAction.ItemChanging Then Console.WriteLine(e.NewValue) Console.WriteLine(e.OldValue) End IfEnd SubI am looking forward to your reply.
Dimitar
Telerik by Progress
Dimitar, thanks for your response. I tested your code and it does indeed show the old and the new value. However, I am noticing that if I switch between cells on the same row and modify both the cells, I am only getting the last cells modification old and new values.
Is there a way to use this logic to be able to obtain the old and new values for each cell in the row itself? Sort of a collection of old and new values to the entire row?
Thank you for writing back.
We are storing the old value, only for the currently edited cell. All other cells have only one value which is the new value in this case. Here is how you can get it:
Private Sub RadGridView1_RowsChanging(ByVal sender As Object, ByVal e As GridViewCollectionChangingEventArgs) If e.Action = NotifyCollectionChangedAction.ItemChanging Then Console.WriteLine(e.NewValue) Console.WriteLine(e.OldValue) For Each item As GridViewCellInfo In radGridView1.CurrentRow.Cells If item.Value IsNot Nothing AndAlso item.ColumnInfo.Name <> e.PropertyName Then Console.WriteLine("New Value: " & item.Value) End If Next item End IfEnd SubLet me know if I can assist you further.
Regards,
Dimitar
Telerik by Progress
I have tried many different scenarios and nothing is working exactly the way I need it to, so I am open to suggestions...
I need an unbound Datagrid with 2 comboxbox columns, Employee (which contains Employee ID and Name) and Pay Code (which contains Pay Code ID and Pay Code Name).
Simply requirements or so I thought...
* For each row, both the Employee and Pay Code MUST be selected from the respective combobox columns for the row to be valid.
* The Employee cannot be duplicated in the grid
* When adding or editing a row, allow User to cancel entry at any time using the Escape Key, especially when Validations fails.
I have tried Cell Validating, RowValidating, RowChanging, and PreviewKeyDown suggestions from various threads reviewed and I still cannot mash it all together to get it exactly the way I need to.
Any help is greatly appreciated.
I think that the best approach for this would be to use the ValueChanging event. This way you will be able to cancel the change if the employee is already added to the grid:
Private Sub RadGridView1_ValueChanging(ByVal sender As Object, ByVal e As ValueChangingEventArgs) If radGridView1.CurrentColumn.Name = "Employee" Then Dim currentValue = e.NewValue For Each item In radGridView1.Rows If item Is radGridView1.CurrentRow Then Continue For End If If DirectCast(currentValue, Integer) = DirectCast(item.Cells("Employee").Value, Integer) Then e.Cancel = True MessageBox.Show("Employee already added") Exit For End If Next item End IfEnd SubI hope this will be useful.
Regards,
Dimitar
Telerik by Progress
Dimitar:
This snippet does indeed prevent the adding of a duplicate Employee. Thank you.
However, it still doe not address the issue of ensuring that
a) Both an Employee and Code was selected so make a "valid" row.
b) Allowing user to cancel the add or edit row entries as needed.
Just using the code provided , the User can add many rows without having a Code selected.
I attempted to use cell validation, but then I get stuck not being able to cancel out of the validation.
UPDATE:
I have been playing around with all day and I am getting really close...
The below is what I have implemented, but I have discovered I am still able to populate a row without an Employee or Code if a use the Tab key
Private Sub gv_Data_ValueChanging(sender As Object, e As ValueChangingEventArgs) Handles gv_Data.ValueChanging If gv_Data.CurrentColumn.Name = "EmployeeID" Then Dim currentValue = e.NewValue For Each item In gv_Data.Rows If item Is gv_Data.CurrentRow Then Continue For End If If String.IsNullOrEmpty(item.Cells("EmployeeID").Value.ToString) = False Then If DirectCast(currentValue, Guid) = DirectCast(item.Cells("EmployeeID").Value, Guid) Then e.Cancel = True MessageBox.Show("Cannot duplicate employee on the Job Crew") Exit For End If End If Next item End If End Sub Private Sub gv_Data_RowValidating(sender As Object, e As RowValidatingEventArgs) Handles gv_Data.RowValidating ' CLEAR ANY CURRENT GUI INDICATION OF ROW ERROR e.Row.ErrorText = String.Empty ' STOP VALIDATION IF THE ESCAPE KEY WAS PRESSED If quitValidating = True Then quitValidating = False Return End If ' PERFORM THE VALIDATIONS If e.Row IsNot Nothing AndAlso e.Row.Cells(1).Value Is Nothing Then e.Row.ErrorText = "Employee selection is required" e.Cancel = True End If If e.Row IsNot Nothing AndAlso e.Row.Cells(2).Value Is Nothing Then e.Row.ErrorText = "Pay Code is required" e.Cancel = True End If End Sub Private Sub gv_Data_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles gv_Data.PreviewKeyDown ' CONTROL THE ROW VALDATION If e.KeyData = Keys.Escape Then quitValidating = True End If End Sub
So again, what am I missing?
You can use the UserAddingRow to check if both values are selected (assuming that all already added rows have valid values):
Private Sub RadGridView1_UserAddingRow(ByVal sender As Object, ByVal e As GridViewRowCancelEventArgs) Dim row = TryCast(e.Rows(0), GridViewNewRowInfo) If row.Cells("Employee").Value IsNot Nothing AndAlso row.Cells("PayCode").Value IsNot Nothing Then Return End If e.Cancel = True MessageBox.Show("All values must be filled")End SubIf the above code is used and only one value is selected the user can press Esc to remove the focus from the new row and go to the already added rows. In this case, it does not matter which key you are using to add the new row.
Regards,
Dimitar
Telerik by Progress
Dimitar:
I utilized RowChanging event with the below code. Is there any added benefit using UserAddingRow versus RowValidating?
01.' PERFORM THE VALIDATIONS02. 03. If e.Row IsNot Nothing AndAlso e.Row.Cells(1).Value Is Nothing Then04. 05. e.Row.ErrorText = "Employee selection is required"06. 07. Me.gv_Data.CancelEdit()08. 09. Me.gv_Data.MasterView.TableAddNewRow.CancelAddNewRow()10. 11. e.Cancel = True12. 13. Exit Sub14. 15. End If16. 17. If e.Row IsNot Nothing AndAlso e.Row.Cells("PayCodeID").Value Is Nothing Then18. 19. e.Row.ErrorText = "Pay code selection is required"20. 21. Me.gv_Data.CancelEdit()22. 23. Me.gv_Data.MasterView.TableAddNewRow.CancelAddNewRow()24. 25. e.Cancel = True26. 27. Exit Sub28. 29. End IfThank you for writing back.
The difference is that once you enter the new row you will not be able to move away from it (cancel the add operation) until both values are set.
In addition, the event will fire for all data rows, not the new row only. This is not necessary if you are sure that all rows have valid values.
I hope this will be useful.
Regards,
Dimitar
Telerik by Progress
Thank you Dimitar for you assistance.
This helped me out tremendously.
