How do I end the editing of a cell in the DataGrid?

If a DataGrid cell is in the process of being edited and the cell doesn't loose focus it may not be flagged as changed. For example, you click a cell, change its value, then click the Save button. The value in the DataTable that's bound to the DataGrid may still contain the original value.

The DataGrid's EndEdit method requests an end to the edit operation taking place. EndEdit takes as parameters the DataGridColumnStyle to cease editing, the number of the row to stop editing and a boolean ShouldAbort flag. Set ShouldAbort True if the edit operation should be stopped.

This snippet tells the DataGrid to continue the edit operation by setting the ShouldAbort flag False. In other words, keep the new value. Then the EndCurrentEdit method of the CurrencyManager ends the edit operation. This code would be placed at the top of your Save button's event handler.

    With DataGrid
        .EndEdit(.TableStyles(0).GridColumnStyles(.CurrentCell.ColumnNumber), _
                 .CurrentCell.RowNumber, False)

        Dim cm As CurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)
    End With

About TheScarms
About TheScarms

Sample code
version info

If you use this code, please mention ""

Email this page

© Copyright 2016 TheScarms
Goto top of page