Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to implement grid level validation in a WPF datagrid. All the examples I can find relate to row and cell level validation.

The rules will apply to a subset of rows per ID where the rows have From and To dates

1. Only 1 row may have To Date Null
2. The current row From Date must not be between any other From / To date pair for this ID
3. Where the To Date is not null then it must not be between any other From / To date pair for this ID.

Can someone point me to a good example?

What I have tried:

I have started looking at how I can pass an instance of the observable collection to a ValidationRule class and started to do online research.
Posted
Updated 7-Jul-20 20:16pm
Comments
[no name] 6-Jul-20 17:00pm    
"Grid level validation" implies a grid-level "operation"; like "Save". Your "save" (or whatever) iterates over the "contents" of the grid (i.e. data source) and saves or it doesn't, showing the most significant / first error if it didn't.

The "current row" in this context has no meaning.

Maybe you're thinking of a "view".
Ger Hayden 6-Jul-20 17:24pm    
Hi Gerry, It has to or I scrap WPF and go back to forms. As I type an idea is forming around rowEndEdit that might just work.
Ger
[no name] 7-Jul-20 10:00am    
If I'm maintaining "lists", I usually use a ListView with usercontrols and use the "SelectedItem" to manage my "current row" (which could be a parent-child relation display). I'll use datagrids with primitive and / or usercontrols to get flexible / parent-child displays, but rarely use them for complex editing. WPF (and UWP) user control list views will outclass any "data grid" (Forms or otherwise).

"Data" Grids were one of the last things added to UWP since UWP's "view" controls sufficed. And that was from a previous "tool kit".
Ger Hayden 8-Jul-20 2:15am    
I wasn't aware of that.

1 solution

Looks like this will do the job:
private void unitDataGridEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    CUnitMasterBase workingRow = (CUnitMasterBase)e.Row.Item;
    CSharedVariables.m_FromToIssuesFlag dateIssues = CSharedVariables.m_FromToIssuesFlag.NONE;

    IEnumerable<CDates> unitDates = new List<CDates>(this.ViewModel.Units
       .Where(i => i.p_Unit_ID == workingRow.p_Unit_ID)
       .Select(d => new CDates ( d.p_Valid_From, d.p_Valid_To )));

    if (unitDates.Count() <= 1)
    {
        return;
    }

    dateIssues = sharedFunctions.Verify_Dates(unitDates, workingRow.p_Valid_From, workingRow.p_Valid_To);

    if (dateIssues !=  CSharedVariables.m_FromToIssuesFlag.NONE)
    {
        unitDataGrid.CancelEdit();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900