views:

230

answers:

1

I have a WPF Dev Express DxGrid that is bound to an ObservableCollection in the following way.

Private _FamilyList As New ObservableCollection(Of FamilyRecord)
MyGrid.DataSource = _FamilyList

When a user starts to enter information in the grid, I need to be able to check whether they have missed some information making it Invalid.

So, what is the best method of checking that the _FamilyList has no validation errors?

+1  A: 

I don't have experience w/ the DevExpress grid, but on the Xceed WPF DataGridControl there's a property called UpdateSourceTrigger which controls when the data source gets updated (when the user is finished editing an entire row, finished editing a cell, or with every key stroke). I'm sure DevExpress has a similar concept.

This will give you control over when the validation happens. You can put your data validation logic in the FamilyRecord class itself. When you detect an error put the FamilyRecord into an error state that will provide a visual cue in the grid.

EDIT:

To determine, upon saving, whether any FamilyRecord objects in your collection have any errors you'd want something like this:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        ObservableCollection<FamilyRecord> _familyRecords;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _familyRecords = new ObservableCollection<FamilyRecord>();
            _familyRecords.Add(new FamilyRecord(@"Jones", false));
            _familyRecords.Add(new FamilyRecord(@"Smith", true));

            comboBox1.ItemsSource = _familyRecords;
        }

        // save button
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // validate whether any records still have errors before we save.
            if (_familyRecords.Any(f => f.HasErrors))
            {
                MessageBox.Show(@"Please correct your errors!");
            }
        }


    }

    public class FamilyRecord
    {

        public FamilyRecord(string name, bool hasErrors)
        {
            Name = name;
            HasErrors = hasErrors;
        }

        public string Name { get; set; }
        public bool HasErrors { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
}
Rob Sobers
Hi Rob, I already have validation logic in the FamilyRecord, and when I Add a row in the grid, it highlights the missing info so I can see that the validation is working. I'm just a bit lost knowing how to check that the collection or grid is in a valid state before allowing it to be saved when the Save button is selected.
Mitch
Ah, I see. How about doing a linq query on the ObservableCollection to see if any are in an error state when the button is clicked?
Rob Sobers
Many thanks Rob, that's really helpful.
Mitch