Question

In Lightswitch by default when you want to delete an item from a on screen List or DataGrid, you can click the delete button provided by default, or you can programmatically delete the item from the VisualCollection by calling in the 'screen code'

this.VisualCollection<Entity>.SelectedItem.Delete()
or
this.VisualCollection<Entity>.DeleteSelcted()

However this marks the selected row/entity for deletion and places an 'X' in the leftmost column of the DataGrid/List. The row remains visible to the user, and while this does reflect the transactional/asynchronous nature of the process, it is confusing to users who expect the row to be removed from the list. For example:

Customer: I deleted it why is it still there...
Me: Did you notice the x to the left?
Customer: Oh.... um...
Me: Yeah... you need to click save for the changes to be persisted to the database.
Customer: ....I'll pretend like that makes sense.
Me: .... that's a good lad ....

A better way would be to remove the item from the VisualCollection when delete is called then silently persist the change. Not having the annoying waiting/loading popup because of the asynchronous nature.

I have tried calling this.VisualCollection<Entity>.RemoveSelected() but that results in a LightSwitchException - Current item cannot be removed

I have tried saving the record after I call Delete() but that saves all changes on screen, and also displays the aforementioned popup and is not a good user experience.

Was it helpful?

Solution

After I make any changes to a DataGrid programatically, I call this function I wrote. It will check for any validation errors and inform the user if they exist so that they can be corrected. Otherwise, it will silently persist the changes in the background. I'm not sure what you mean by "waiting/loading popup". The only indication is the small blue spinner next to the screen name on the tab for a second or two.

private void ValidateAndSave()
{
    //Check for validation errors
    if ((this.Details.ValidationResults.HasErrors == false)) {
        //Save the changes to the database
        try {
            this.DataWorkspace.DatabaseNameData.SaveChanges();
        } catch (Exception ex) {
            this.ShowMessageBox(ex.ToString());
        }
    } else {
        //If validation errors exist,
        string res = "";
        //Add each one to a string,
        foreach (object msg_loopVariable in this.Details.ValidationResults) {
            msg = msg_loopVariable;
            res = res + msg.Property.DisplayName + ": " + msg.Message + "\r\n";
        }

        //And display them in a message box
        this.ShowMessageBox(res, "Validation error", MessageBoxOption.Ok);
    }
}

Note: I converted this from VB.NET so it's probably not a drop in replacement. In particular I think the Message Box is done differently so double check that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top