Home Forums WPF controls Xceed DataGrid for WPF Getting the current Row to call EndEdit

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
  • User (Old forums)
    Post count: 23064
    #22093 |

    I can’t find how to get the current selected Row in my DataGridControl (or TableView, I don’t know).
    The goal of this is to call the EndEdit method in a custom CellEditor. I would like the editing to stop when I decide, and not when the user press the “Enter” key…

    Is there a way to do this? If yes, how?
    Thanks a lot.

    Imported from legacy forums. Posted by Tanguy (had 6464 views)

    Xceed Support
    Post count: 5658


    The reason why the current “DataRow” is not immediately accessible is that we are doing UI Virtualization. Because of this, keeping references to a DataRow (and even cells) is “dangerous”…

    However, for the need you specified, be aware that it is possible to retrieve the container (i.e DataRow) for the current item by doing the following:

    DataRow myRow = myDataGridControl.ItemContainerGenerator.ContainerFromItem( myDataGridControl.CurrentItem ) as DataRow;

    In addition would you like to disable the “Enter” key reaction?

    Imported from legacy forums. Posted by Marcus [Xceed] (had 449 views)

    User (Old forums)
    Post count: 23064

    Thanks for your answer.

    I don’t actually want to disable the “Enter” key reaction, (you can tell me how to do, if I need it one day 😉 ), I’ll give you a concrete example of what I want to do:

    I have a cell type that is a ComboBox. Each item of my Combobox is “templatized” (with an image, a disabled checkbox, a text). The data binded to this cellType is the SelectedIndex of my ComboBox.

    So, when my user has selected the right line in the ComboBox, the drop down list disapears, but today you need to press enter to validate the Binding (the binded data “MyComboBox.SelectedIndex” remains to the old value until you press enter, or edit another cell).

    For me, this is a problem, because my grid is a part of my UI, and just after selecting a value in a combobox of the grid, my user can click on a button that save datas for example. This will not take care of the last ComboBox changed value…

    I hope I explained it successfully :~ Maybe you have advices for me regarding what I described above?

    Imported from legacy forums. Posted by Tanguy (had 645 views)

    Xceed Support
    Post count: 5658

    Yes, I think I understand your problem.

    Can you post the code you are using the create your ComboBox editor?

    Imported from legacy forums. Posted by Marcus [Xceed] (had 395 views)

    User (Old forums)
    Post count: 23064

    Yes, of course I can 😉

    Here it is

    <DataTemplate x:Key =”MyEditor”>
    <ComboBox SelectedIndex =”{xcdg:CellEditorBinding}”
    ItemsSource =”{Binding Source={StaticResource MyDatabase}, XPath=Unit}”
    ItemTemplate =”{StaticResource MyRow}”
    <b>SelectionChanged =”Grid_EndEdit”</b>
    BorderThickness =”0″

    I’ve coded the “Grid_EndEdit” with the snippet you gave me, I let you tell if it is the best way to do
    public void Grid_EndEdit(object sender, RoutedEventArgs e)
    if (grid.CurrentItem == null)
    Xceed.Wpf.DataGrid.DataRow row = grid.ItemContainerGenerator.ContainerFromItem(grid.CurrentItem) as Xceed.Wpf.DataGrid.DataRow;
    if (row != null)

    With this, it works perfectly. I had to put it in a Try/Catch statement to avoid Exceptions when sorting columns, but it works.

    Imported from legacy forums. Posted by Tanguy (had 593 views)

    Xceed Support
    Post count: 5658

    So, just to make sure I understand the problem right:

    If a user opens the ComboBox, and just change the selection in the list (without closing the drop-down), the SelectionChanged callback is not called… Which causes the problem you mention (clicking the Save button does not save the proper value).

    If I nailed it… Then I might have a workaround for you…

    Since the edition is not “completed” when you are doing the MouseDown on the “Save” button, you could force it to be completed for the case of the “Save” button…

    Here is how I would do it:

    1. In the handler for the Save button, add a verification that ensures that the edition for the current row is completed before saving the data (same code as the one from your Grid_EndEdit handler).

    Alternately, if modifying the code for the Save button is not a possibility:

    1. Register to the PreviewMouseLeftButtonDown on the window/page.
    2. In the handler, verify that the Source/OriginalSource of the event is the Save button.
    3. If it comes from the Save button, end edition on the DataRow (same code, once again).

    If I am completely off-target, then I think I would need a little more explanation. :~

    Imported from legacy forums. Posted by Marcus [Xceed] (had 680 views)

    User (Old forums)
    Post count: 23064

    You nailed it 😉
    Ending the edition of the row on the “SelectionChanged” event of the combobox solved the problem, but verifying that the row edition is finished in the SaveButton handler is Safer.

    Thanks a lot.

    Imported from legacy forums. Posted by Tanguy (had 464 views)

    Xceed Support
    Post count: 5658


    Just wanted to drop-in on you concerning the issue where the row currently in edition does not leave edition mode when focus is moved elsewhere in the window/page.

    After analyzing this thoroughly, we arrived at the conclusion that it would be best fitted NOT to automatically “end” edition on the currently edited row when the DataGridControl loses focus.

    The 2 primary reasons behind this are:

    – We cannot assert what are the needs of each and every user. It might be possible that some people would like to perform actions in another control based on the row currently in edition ( I have no specific examples yet ).

    – Since we have a validation scheme on the DataGridControl, automatically ending an edition could result in a validation error (DataGridValidationException). Under such circumstances, the row in edition would remain in edition (and any actions performed on the data would be done on the “non-edited”, without any particular indications of the problem).

    However, we acknowledge that we need a solution that allows users that encountered the same problem as you have to overcome it easily.

    The fix we are proposing is to add functions on the DataGridControl class to allow easily ending edition ( or cancelling it), without the need to fetch the DataRow for the CurrentItem.

    This would allow ensuring that if you need “up-to-date” data, you’d have it easily. You’d also be able to easily detect that edition cannot be “ended” (DataGridValidationException) and decide whether or not you want to proceed with your action.

    If this solution is problematic for your needs, we’d like to hear from you. This is to make sure that we are not leaving specific needs “un-addressed”.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 7223 views)

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.