Refreshing data in Grid during row edit

Apr 21, 2009 at 10:19 PM
I've got a grid which represents an address. It's columns are Line1, line2, city, state, zip and a Zipcode lookup button. I want the user to enter Line1 & Line2 first, then tab to the zip field, enter teh zip code, and then press the Zipcode lookup button, which, if the city & state are found, will populate those fields. 

The problem I'm having is when they click the Zipcode lookup button, it populates the underlying object (so that when I save it, the City & state persist in the database), but on the screen those columns are blank on the grid.  If I call refresh on the grid, I get an exception that I can't refresh during a row edit.

What's the best way to do this? Refresh the data after the row has been edited?

Here's the code: 



void ZipLookup(object sender, ExecutedRoutedEventArgs e)




// if ZipLookup is called from the PolicyHolder grid, // get the ZipCode from the Zip textbox, and



// use the LookupAddress to set the appropriate fields on there



if (((System.Windows.FrameworkElement)e.Source).Name == dgProperties.Name)




Property newProperty = dgProperties.SelectedItem as Property;



if (newProperty.Zip == string.Empty)




CoreUI.ShowMessage("Please enter a ZIP code to lookup.");








LookupAddress address = LookupAddress.LookupPostalCode(newProperty.Zip);



if (address != null)



newProperty.City = address.City;

newProperty.State = address.State;

newProperty.Zip = address.ZipCode;











Apr 22, 2009 at 1:12 PM
The easiest way to solve this is probably to implement INotifyPropertyChanged so that the datagrid will know that the value of the property has changed (which will cause it to refresh its display of the data). If when you tab to one of the blank columns and enter edit mode on it the correct data appears, that's very likely your problem.
Apr 22, 2009 at 4:19 PM
Hi Mikj,

Thanks for your help. I'm currently binding an observable collection to this grid, so it would be a big change to implement the INotifyPropertyChanged interface, but one that is doable as a last resort.

However, I'm not getting the behavior that you're saying- I change the value of the datagridcell's bound property in Codebehind when a user clicks a button. But when tabbing away to other columns on the grid, or even other controls on the page, it does not refresh (the data is not displayed). Only when I leave the page and come back in is the data displayed.

Any ideas other than the INotifyPropertyChanged event?