locked
Datagrid Cancel Cell Edit RRS feed

  • Question

  • Hi, I have a beginners question about the datagrid control that I hope someone can help me with.  When I set the cancel property of the e argument in the CellEditEnding event to "true" nothing happens.  The cell contents remains and the cursor is locked in the cell.  In other words, no other cell, or control on the page can receive focus.  The event is being fired each time I click in another cell or press the Tab key. 

    I also tried calling the CancelEdit() method on the datagrid, in the CellEditEnding event, which caused the event to be fired over 4,000 times then throw an overflow exception.

    Here is the xaml code for the datagrid, and the code behind.

    <Grid x:Name="LayoutRoot" Background="White" Width="520" Height="300">
        <StackPanel>
            <data:DataGrid x:Name="accountsDataGrid" AutoGenerateColumns="False" CellEditEnding="accountsDataGrid_CellEditEnding">
                <data:DataGrid.Columns>
                    <data:DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
                    <data:DataGridTextColumn Binding="{Binding Email}" Header="Email"/>
                    <data:DataGridTextColumn Binding="{Binding City}" Header="City"/>
                    <data:DataGridTextColumn Binding="{Binding Pincode}" Header="Pin Code"/>                
                </data:DataGrid.Columns>                        
         </data:DataGrid>
            <data:DataPager x:Name="accountsPager" PageSize="4" DisplayMode="FirstLastNumeric"/>
            <CheckBox Content="Filter by New Jersey" Click="CheckBox_Click"></CheckBox>
        </StackPanel>        
    </Grid>
     
    private void accountsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        // If this code is used the ouput window displays 4,351 with this error:
        // An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.dll
    counter++;
        Debug.WriteLine(counter);
        DataGrid tDataGrid = sender as DataGrid;
        tDataGrid.CancelEdit();

        
        // I tried this first without the above code and nothing happens to the cell, but the cursor is locked in the cell      
        e.Cancel = true;
    }
     
     Thanks for anyones help in advance.
    Sunday, September 5, 2010 12:07 PM

Answers

  • a) "e.Cancel = true" will cancel "cell edit ending," in other words you are telling it that the cell should stay in edit mode.

    b) "tDataGrid.CancelEdit();" triggers a call to "accountsDataGrid_CellEditEnding()." Check this with a flag as such:

    private bool isEditEnding = false;
    private void accountsDataGrid_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
    {
        if (isEditEnding)
        {
            return;
        }
    
        try
        {
            isEditEnding = true;
    
            // Do all your custom checks here...
    
            // Cancels the current cell instead of the current row, which is the default action.
            (sender as DataGrid).CancelEdit(DataGridEditingUnit.Cell);
        }
        finally
        {
            // Inside a "finally" to ensure that it will always get called, even if there is an exception.
            isEditEnding = false;
        }
    }
    


    Please "mark as answer" if this helped.


    Wednesday, September 8, 2010 3:32 AM

All replies

  • http://betaforums.silverlight.net/forums/t/198368.aspx

    Wednesday, September 8, 2010 2:23 AM
  • a) "e.Cancel = true" will cancel "cell edit ending," in other words you are telling it that the cell should stay in edit mode.

    b) "tDataGrid.CancelEdit();" triggers a call to "accountsDataGrid_CellEditEnding()." Check this with a flag as such:

    private bool isEditEnding = false;
    private void accountsDataGrid_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
    {
        if (isEditEnding)
        {
            return;
        }
    
        try
        {
            isEditEnding = true;
    
            // Do all your custom checks here...
    
            // Cancels the current cell instead of the current row, which is the default action.
            (sender as DataGrid).CancelEdit(DataGridEditingUnit.Cell);
        }
        finally
        {
            // Inside a "finally" to ensure that it will always get called, even if there is an exception.
            isEditEnding = false;
        }
    }
    


    Please "mark as answer" if this helped.


    Wednesday, September 8, 2010 3:32 AM
  • PS: You probably know this already, but if you are trying to do custom validation then this is not the way to do it!!! You should bind the grid to an object and check the value on the property setter. In SL 3 you should throw an exception, which will show a nice red border. For SL 4, take a look at these links:

    http://hi.baidu.com/freezesoul/blog/item/e5da5aee4066aef6b2fb9503.html

    http://johnpapa.net/silverlight/enabling-validation-in-silverlight-4-with-idataerrorinfo/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+JohnPapa+%28JohnPapa.net%29

    http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx

    Wednesday, September 8, 2010 3:48 AM
  • Thanks so much for the help....

    You have confirmed what I thought was the case in your first explanation.  I was trying to follow the steps in a training book, and the results of setting e.Cancel = true were not what the author had suggested, but I wanted to make sure my assumptions were correct.

    As for b), I was missing the DataGridEditingUnit.Cell parameter in CancelEdit.  I noticed the signature of the method in intellisence, but could not find an example of what to send it!  Thanks for sharing your code with me!

     

     

    Wednesday, September 8, 2010 5:54 PM
  • Yes I'm becoming very aware of how SL drives developers to do validation in the business layer.  But thanks for the links, I will definately check them out for further instructions.

    Wednesday, September 8, 2010 5:56 PM
  • Glad to be of help :-)

    Thursday, September 9, 2010 8:53 AM
  • Thank you. I solved my problem due to you.
    Saturday, February 10, 2018 1:42 AM