Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > using IDataErrorInfo to validate bound complex/aggregate types.
 

using IDataErrorInfo to validate bound complex/aggregate types.

I have a fairly simple object:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address MailingAddress { get; set; }
}
Where Address is defined like so:
public class Address
{
    public string StreetAddress { get; set; }
    public string PostalCode { get; set; }
    public string Province { get; set; }
    public string PostalCode { get; set; }
}
Now, I'm binding my UI as shown here, and my IDataErrorInfo looks like this:

public string this[string columnName]
{
    get
    {
        string result = null;

        // the following works fine
        if(columnName == "FirstName")
        {
            if (string.IsNullOrEmpty(this.FirstName))
                result = "First name cannot be blank.";
        }
        // the following does not run 
        // mostly because I don't know what the columnName should be
        else if (columnName == "NotSureWhatToPutHere")
        {
            if (!Util.IsValidPostalCode(this.MailingAddress.PostalCode))
                result = "Postal code is not in a know format.";
        }
        return result;
    }
}
So, obviously, I don't know what the columnName should be. I've tried "MailingAddress.PostalCode" and I've even tried running something like if (columnName.ToUpperInvariant().Contains("POSTAL")) System.Windows.Forms.MessageBox.Show("Validating Postal Code"); but to no avail.

Can I not validate complex or aggregate types via IDataErrorInfo, or is there something I'm missing?
SnOrfus  Monday, September 21, 2009 5:30 PM
In thinking about this more, I assume you have this:

public class Person : IDataErrorInfo

And not just the declaration as in your code segment?

I use a base class to implement IDataErrorInfo (That is here if you are interested: http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx)

But I would guess that you would also need

public class Address : IDataErrorInfo

You could then validate the address in the Address class.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, September 22, 2009 4:31 AM
Is this WPF binding?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, September 22, 2009 3:41 AM
In thinking about this more, I assume you have this:

public class Person : IDataErrorInfo

And not just the declaration as in your code segment?

I use a base class to implement IDataErrorInfo (That is here if you are interested: http://msmvps.com/blogs/deborahk/archive/2009/07/21/building-a-business-object-base-class.aspx)

But I would guess that you would also need

public class Address : IDataErrorInfo

You could then validate the address in the Address class.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, September 22, 2009 4:31 AM

You can use google to search for other answers

Custom Search

More Threads

• memory leak in datagrid binding and unexpected behaviours
• Treeview node selected loads text file to TextBox
• Queue
• Find Value in a DGV Cell
• placing images in DataGridView control
• Saving Data from a datagrid to a dataset
• in windows application i want to use a notify icon which should be
• seting date to Null
• Adding new rows to DataGridView does not follow sort order
• Disapperance of the form controls