I built a CRUD interface using a ListBox to try and simulate a datagrid—I didn’t really like the standard Datagrid control. However, I’m struggling to validate the cells in each row. I’m not sure if I’m doing it wrong. Delete and Add work fine, but I’m having trouble with validation for new cells and updates. Could anyone share an example that works well? I’d really appreciate it.
What do you mean with validation?
If you are talking about testing that the record has internal consistency? I would wait until the user chooses to save it (or navigates away, if that is a save point).
At that point check the fields together prior to saving. If you try and check it field by field you will find all sorts of issues such as, fields that are yet to be entered which will complicate the validation.
A major fix is to not allow a user to enter invalid data in the first place. For example if the question is “Male” or “Female”, or any similar discrete value choice, always use a pop up menu with only those choices. Don’t ever allow them to type into the field. You don’t have to display data that way, just enter it.
Often an interface will provide a “form” for data editing. It can be better for the user to see a page with just the single record than the data strung out across a thin line. If you are entering new data, you can provide a “Save and enter another” type button. This saves having to open and close the dialog over and over for multiple records.
validate if the text will not duplicate, phone number is valid, value is valid, etc..
That what i do not want to use (another form to validate). The validattions like if the record already inserted in to the table, phone number, sex (Male/Female), etc…
but i want to do this in the listbox, not when i will save it to the database.
i created some methods to do it, but it’s now working well. I would like to rewrite everything and 'm looking for something that works fine.
DesktopListbox has several Cell* events. I think CellAction would be what you want - it fires when the user finishes editing a cell.
Sorry, I did not mean you had to use a form. Only that some people do.
That said, there must be a point when you save the record to the database. Surely you are not doing that on a per field basis?
At that point I would have a method that validates all the fields in one go. Reports an issue or saves the new record. The first thing I would do is to ensure all mandatory fields are completed. Then move on to any field level tests. Finally any multi field tests.
I would still stand by my recommendation for using pop up menus or radio button when entering discrete choices. Allowing free entry, when it can only be Male or Female etc is just asking for problems. Even if you do it in line. You don’t have to validate what is already controlled.
And then, what happens when the validation fails ?
This would be done using a combination of the DesktopListBox’s events. Specifically, CellAction, CellPressed, CellTextChanged, CellKeyDown, and others. You could perform your validation by accessing the DesktopListBox.ActiveTextEditor.
A couple of examples from something I worked on recently:
Sub CellAction(row As Integer, column As Integer) Handles CellAction
select case self.ColumnTypeAt( column )
case DesktopListBox.CellTypes.Default, DesktopListBox.CellTypes.TextField
var value as String = self.CellTextAt( row, column )
self.CellTextAt( row, column ) = value.ToDouble.ToString( "#0.0#############" )
RaiseEvent CellValueChanged( row, column )
case DesktopListBox.CellTypes.CheckBox
RaiseEvent CellValueChanged( row, column )
case else
MessageBox( "Column Type not implemented!" )
end select
'// This is a workaround due to DesktopListBox editing being a bit broken.
timer.CallLater( 1, WeakAddressOf self.SetFocus )
End Sub
Sub CellTextChanged(row As Integer, column As Integer) Handles CellTextChanged
if self.ActiveTextControl.Text.IsNumeric = False then
self.ActiveTextControl.BackgroundColor = &cff0000
else
self.ActiveTextControl.BackgroundColor = color.FillColor
end if
End Sub
Building a spreadsheet-like editing mechanism is no small feat, and not something that could be thrown together in a few moments for an example to be posted freely here, though someone may have something ready to go that they could toss up.