Home » SilverlightRSS

Datagrid cell navigation with enter key

hi all,

How to navigate the cursor to rightwards in a datagrid instead of the default downwards while we press the enter key.?


Thanks in advance.

Saji

 

4 Answers Found

 

Answer 1

Hi


you can do something like this,

public static class DataGridExtensions
    {
        public static void NextCellToEdit(this DataGrid grid)
        {
            if (grid.SelectedItem != null)
            {
                int nextColumnIndex = GetNextAvailableColumnIndex(grid);
                grid.CurrentColumn = grid.Columns[nextColumnIndex];
                grid.ScrollIntoView(grid.SelectedItem, grid.CurrentColumn);
                grid.BeginEdit();
            }
        }
        static int GetNextAvailableColumnIndex(DataGrid grid)
        { 
            return GetNextAvailableColumn(grid).DisplayIndex;
        }
        public static DataGridColumn GetNextAvailableColumn(this DataGrid grid)
        {
 
            int nextColumnIndex = grid.CurrentColumn.DisplayIndex + 1;
 
            if (nextColumnIndex >= grid.Columns.Count)
            {
                nextColumnIndex = 0;
            }
 
            while (grid.Columns[nextColumnIndex].IsReadOnly || grid.Columns[nextColumnIndex].Visibility == Visibility.Collapsed)
            {
                if (nextColumnIndex < grid.Columns.Count - 1)
                {
                    nextColumnIndex++;
                }
                else
                {
                    nextColumnIndex = 0;
                }
            }
 
            return grid.Columns[nextColumnIndex];
        }
        public static bool IsLastCell(this DataGrid grid)
        {
 
            int currentColumnIndex = grid.CurrentColumn.DisplayIndex;
 
            if (currentColumnIndex == grid.Columns.Count - 1)
            {
                return true;
            }
            else
            {
                for (int index = currentColumnIndex + 1; index <= grid.Columns.Count - 1; index++)
                {
                    if (!grid.Columns[index].IsReadOnly)
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        public static bool IsLastRow(this DataGrid grid)
        {
            PagedCollectionView source = new PagedCollectionView(grid.ItemsSource);
 
            if (source != null)
            {
                return (grid.SelectedIndex == source.TotalItemCount - 1 ? true : false);
            }
            else
            {
                return true;
            }
        }
 
    }

And add an KeyDown event to your datagrid


private void myDatagrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Tab) || e.Key.Equals(Key.Enter))
            {
                e.Handled = true;

                if (myDatagrid.CurrentColumn != null)
                {
                    if (myDatagrid.IsLastRow() & myDatagrid.IsLastCell()) // Last Row And Last Cell (End of the grid)
                    {
                        myDatagrid.CommitEdit();

                        // TODO : Add new row to the datagrid

                        myDatagrid.SelectedIndex = myDatagrid.SelectedIndex + 1;
                        myDatagrid.NextCellToEdit();
                    }
                    else if (myDatagrid.IsLastCell()) // Last Cell
                    {
                        myDatagrid.SelectedIndex = myDatagrid.SelectedIndex + 1;
                        myDatagrid.NextCellToEdit();

                    }
                    else // Normal navigation
                    {
                        myDatagrid.NextCellToEdit();
                    }

                }
            }

        }

Regards

 

Answer 2

Hi Xlinspire,

Thank you so much for your code. I tried that code but it is not working. My datagrid_KeyDown event is not firing while i press the keys  like Tab,Enter,PageUp,PageDonwn,Home,End,UP,Down,Left and Right keys. Almost other keys are firing the keyDown event. 

I am trying to develop an Out Of Browser application with Silverlight 4 with VS 2010. Could you please give me any information how the "DataGridExtension" Class works?

Thanks for your help.

Regards,

Saji



 

Answer 3

hi,

Saji dk:
Could you please give me any information how the "DataGridExtension" Class works?


The ideia behind all, is to find the next avaiable column, if we find readonly or invisible columns are ignored. Then, if there isn't no next column the grid have more rows, we go to next row and we repeat the routine until is the last row, if we are at the last row and at the last column we can add another row (using another workaround).

This is usefull in many scenarios.


however you are right, you only can handle some keys if you extend the control datagrid and override the "OnKeyDown". This solution should work.


    public static class DataGridExtensions
    {
        public static void NextCellToEdit(this DataGrid grid)
        {
            if (grid.SelectedItem != null)
            {
                int nextColumnIndex = GetNextAvailableColumnIndex(grid);
                grid.CurrentColumn = grid.Columns[nextColumnIndex];
                grid.ScrollIntoView(grid.SelectedItem, grid.CurrentColumn);
                grid.BeginEdit();
            }
        }

        static int GetNextAvailableColumnIndex(DataGrid grid)
        {
            return grid.GetNextAvailableColumn().DisplayIndex;
        }

        public static DataGridColumn GetNextAvailableColumn(this DataGrid grid)
        {

            int nextColumnIndex = grid.CurrentColumn.DisplayIndex + 1;

            if (nextColumnIndex >= grid.Columns.Count)
            {
                nextColumnIndex = 0;
            }

            while (grid.Columns[nextColumnIndex].IsReadOnly || grid.Columns[nextColumnIndex].Visibility == Visibility.Collapsed)
            {
                if (nextColumnIndex < grid.Columns.Count - 1)
                {
                    nextColumnIndex++;
                }
                else
                {
                    nextColumnIndex = 0;
                }
            }

            return grid.Columns[nextColumnIndex];
        }
        public static bool IsLastCell(this DataGrid grid)
        {

            int currentColumnIndex = grid.CurrentColumn.DisplayIndex;

            if (currentColumnIndex == grid.Columns.Count - 1)
            {
                return true;
            }
            else
            {
                //Check if exists an not readonly cell available
                for (int index = currentColumnIndex + 1; index <= grid.Columns.Count - 1; index++)
                {
                    if (!grid.Columns[index].IsReadOnly)
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        public static bool IsLastRow(this DataGrid grid)
        {
            PagedCollectionView source = new PagedCollectionView(grid.ItemsSource);

            if (source != null)
            {
                return (grid.SelectedIndex == source.TotalItemCount - 1 ? true : false);
            }
            else
            {
                return true;
            }
        }

    }

    public class MyCustomDataGrid : DataGrid
    {

        public MyCustomDataGrid()
        {
            DefaultStyleKey = typeof(DataGrid);
        }
        
        protected override void OnKeyDown(KeyEventArgs e)
        {

            if (e.Key.Equals(Key.Tab) || e.Key.Equals(Key.Enter))
            {
                e.Handled = true;

                if (CurrentColumn != null)
                {
                    if (this.IsLastRow() & this.IsLastCell()) // Last Row And Last Cell (End of the grid)
                    {
                        CommitEdit();
                        SelectedIndex = SelectedIndex + 1;
                        this.NextCellToEdit();
                    }
                    else if (this.IsLastCell()) // Last Cell
                    {
                        SelectedIndex = SelectedIndex + 1;
                        this.NextCellToEdit();

                    }
                    else // Normal navigation
                    {
                        this.NextCellToEdit();
                    }

                }
            }
            else
            {
                base.OnKeyDown(e);
            }

        }

    }



you should use the control MyCustomDataGrid instead of DataGrid.


Regards


 

Answer 4

Hi XlInspire,

Thank you so much your code and explanation. It is working well.


Regards,

Saji

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter