DataGrid CellEditEnding with messageBox fires infinitely

May 18, 2009 at 2:37 PM

In my datagrid, I've got an event wired up on the CellEditEnding event that executes some code to lookup if the value they entered is valid or not (comparing against Zip codes).

If the data they enter is invalid, I show a messagebox.

The problem I'm having is that the messagebox continually fires if it's invalid (I think the focus is switching between the cell and the messagebox) and gets caught in a bad loop?

Any ideas with this?

Thanks,

Chris

May 18, 2009 at 3:26 PM

You may want to look into the validation framework instead. I think Colin E. had an example of doing cell-level validation on his WPF blog. http://www.scottlogic.co.uk/blog/wpf

Jun 3, 2009 at 9:43 AM

I used Dispatcher.BeginInvoke(...) to show the message box can solve the problem. You can try it.

Jul 14, 2009 at 12:00 PM

I am running into similar issue. I did try putting my MessageBox in Dispather.BeginInvoke() as mentioned above but that didn't help either.

Here is my code sample:

private void DataGridEntities_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            
            TextBox TextBoxGridCell = e.EditingElement as TextBox;
            if (TextBoxGridCell != null)
            {
                string ValueInTextBox = TextBoxGridCell.Text.Trim();
                if (string.IsNullOrEmpty(ValueInTextBox))
                {
                    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
                                {
                                    MessageBox.Show("Error in grid cell");
                                }));
                                    
                    TextBoxGridCell.SelectAll();//.Focus();
                }
            }
            
        }

Any help would be highly appreciated.

Thanks

Gaurav Mantri