Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
450
Check if the currently selected cell is the very last in the grid
posted

Hello,

I'd like to check whether the user has currently the last (= most bottom right) cell in the grid selected, so I can jump back the the first cell in the grid if he tabs further.

I'm currently doing it like this. However this doesn't work if the user has altered the order of the columns and moved them around, since the last cell seems to always stay the same, even if it has been moved.

private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    XamDataGrid grid = (XamDataGrid) sender;
    if (e.Key == Key.Tab || e.Key == Key.Enter)
    {
       if(grid.ActiveCell == ((DataRecord)grid.Records[grid.Records.Count-1]).Last())
        {
           grid.ExecuteCommand(DataPresenterCommands.CellFirstOverall);
         }
     }
 }

Is there maybe any "better" way to do this?

Parents
  • 35319
    posted

    Hi,

     

    I have been looking into your question and the issue in your scenario is that the field index is not changed when the field position is changed. In order to able to check the actual field position of the active cell you could you the following ‘if’ statement:

     

    if (grid.ActiveCell.Record.Index == grid.Records.Count - 1 && grid.ActiveCell.Field.ActualPosition.Column == grid.FieldLayouts[0].Fields.Count - 1)

     

    I am attaching a sample application(datagridActiveCellNavigation.zip) that shows my suggestion.

     

    Let me know, if you need any further assistance on this matter.

     

     

    datagridActiveCellNavigation.zip
Reply Children