Welcome to the Xceed Community | Help
Community Search  
More Search Options

DataGrid How to use the Enter key to move to the first column after insert

Sort Posts: Previous Next
  •  04-28-2009, 11:32 PM Post no. 20617

    Gift [G] DataGrid How to use the Enter key to move to the first column after insert

    I have had a problem that I have finally found a solution.

    My users require the ability for the focus to be moved to the first column of the next data row when the press enter on the insertion row. They are happy to use the arrow keys to move around the grid and the tab key whilst in edit mode but when they pressed the Enter key the were very unhappy the focus remained on the column that had the focus when the pressed the Enter key. This meant they would have to either left mouse click into the first column or move there by use of arrow keys etc...  I like to keep my users happy so here is my attempt at moving the focus to first column after the Enter key has been pressed.

    Please note; this currently only works when a user inserts a new row but that is by design.

    Xaml

    <xcdg:DataGridControl Name="TestGrid"

                          PreviewKeyUp="TestGrid_PreviewKeyUp"

                          xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">

        <xcdg:DataGridControl.View>

            <xcdg:TableView UseDefaultHeadersFooters="False">

                <xcdg:TableView.Footers>

                   <DataTemplate>

                        <xcdg:InsertionRow />

                   </DataTemplate>

                </xcdg:TableView.Footers>

                <xcdg:TableView.FixedHeaders>

                   <DataTemplate>

                        <xcdg:HierarchicalGroupByControl xcdg:TableView.CanScrollHorizontally="False" />

                   </DataTemplate>

                   <DataTemplate>

                        <xcdg:ColumnManagerRow />

                   </DataTemplate>

                </xcdg:TableView.FixedHeaders>

            </xcdg:TableView>

        </xcdg:DataGridControl.View>

    </xcdg:DataGridControl>

     

    VB Code

        Private Sub TestGrid_PreviewKeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)

     

            If e.Key = Key.Enter Then

                Dim grid As DataGridControl = sender

                Dim c As Column = grid.CurrentColumn

                Dim row As Integer = grid.SelectedIndex

                If c IsNot Nothing And c.HasValidationError = False And row = -1 Then

                    Dim pos As Integer = grid.VisibleColumns.IndexOf(c)

     

                    If pos = 0 Or pos = grid.Columns.Count - 1 Then

     

                        c = grid.Columns(0)

                        grid.CurrentColumn = c

                        grid.BringItemIntoView(c)

                        grid.BeginEdit()

                        e.Handled = True

                    Else

                        e.Handled = False

                    End If

                Else

                    e.Handled = False

                End If

                End If

        End Sub

    If will post changes to move the focus to the first column of the next row when the user presses the Enter key on an existing row, as soon as I figure out how to do that.

     

  •  04-28-2009, 11:54 PM Post no. 20618 in reply to 20617

    Re: DataGrid How to use the Enter key to move to the first column after insert

    Here are some changes to the VB code to allow for the user to press Enter key on an existing row and for the focus to move to the first column of the next row.  Still don't know how move focus to the insertion row, so if anyone has any ideas they will be very well received.

    VB Code

        Private Sub TestGrid_PreviewKeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)

     

            If e.Key = Key.Enter Then

                Dim grid As DataGridControl = sender

                Dim c As Column = grid.CurrentColumn

                Dim row As Integer = grid.SelectedIndex

     

                If c IsNot Nothing And c.HasValidationError = False And row = -1 Then

                    Dim pos As Integer = grid.VisibleColumns.IndexOf(c)

     

                    If pos = 0 Or pos = grid.Columns.Count - 1 Then

     

                        c = grid.Columns(0)

                        grid.CurrentColumn = c

                        grid.BringItemIntoView(c)

                        grid.BeginEdit()

                        e.Handled = True

                    Else

                        e.Handled = False

                    End If

                Else

                    If c IsNot Nothing And c.HasValidationError = False And row <> -1 Then

                        c = grid.Columns(0)

                        grid.CurrentColumn = c

                        grid.Items.MoveCurrentToNext()

                        e.Handled = True

                    Else

                        e.Handled = False

                    End If

                End If

            End If

        End Sub

  •  04-29-2009, 4:58 PM Post no. 20635 in reply to 20618

    Re: DataGrid How to use the Enter key to move to the first column after insert

    Hi Stewart,

    I solved in this way, first deriving the Insertion row and overriding the OnPreviewKeyDown event, then using the derived insertion row in xaml, it works for me.

        public class DerivedInsertionRow : Xceed.Wpf.DataGrid.InsertionRow
        {
            protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
            {
                base.OnPreviewKeyDown(e);

                if (e.Key == Key.Enter)
                    this.Cells[0].BeginEdit();
            }
        }
     

    [cut]

                <xcdg:DataGridControl.View>
                    <xcdg:TableView UseDefaultHeadersFooters="False">
                        <xcdg:TableView.Footers>
                            <DataTemplate>
                                <!--<xcdg:InsertionRow />-->
                                <views:DerivedInsertionRow/>
                            </DataTemplate>
                        </xcdg:TableView.Footers>

                    </xcdg:TableView>
                </xcdg:DataGridControl.View>
    [cut]

     

    Note to activate the followin behaviour at the grid level

                CellEditorDisplayConditions="CellIsCurrent"
                EditTriggers="BeginEditCommand, CellIsCurrent, ActivationGesture"
     

    Hope this helps.

    Antonio

     

  •  04-30-2009, 9:09 PM Post no. 20662 in reply to 20635

    Re: DataGrid How to use the Enter key to move to the first column after insert

    Thank you Antonio for your suggestion.

    However, I thought I would tackle the problem so as to provide the solution 100% in the code behind and within the one subroutine.  The changes simply move the focus down one row only when we are one the last row. I have tested this with or without an insertion row and all works just fine.

    So here is the lastest installment of PreviewKeyUp subroutine.

        Private Sub TestGrid_PreviewKeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)

     

            If e.Key = Key.Enter Then

                Dim grid As DataGridControl = sender

                Dim c As Column = grid.CurrentColumn

                Dim row As Integer = grid.SelectedIndex

                If c IsNot Nothing And c.HasValidationError = False And row = -1 Then

                    Dim pos As Integer = grid.VisibleColumns.IndexOf(c)

     

                    If pos = 0 Or pos = grid.Columns.Count - 1 Then

                        c = grid.Columns(0)

                        grid.CurrentColumn = c

                        grid.BringItemIntoView(c)

                        grid.BeginEdit()

                        e.Handled = True

                    Else

                        e.Handled = False

                    End If

                Else

                    If c IsNot Nothing And c.HasValidationError = False Then

                        c = grid.Columns(0)

                        grid.CurrentColumn = c

                        If row = grid.Items.Count - 1 Then

                            Dim focusedElement As UIElement = Keyboard.FocusedElement

                            focusedElement.MoveFocus(New TraversalRequest(FocusNavigationDirection.Down))

                            grid.BringItemIntoView(c)

                        Else

                            grid.Items.MoveCurrentToNext()

                        End If

                        e.Handled = True

                    Else

                        e.Handled = False

                    End If

                End If

            End If

        End Sub

    I hope the provision of this subroutine makes someone's day a little easier.

    Stewart

  •  09-01-2009, 12:33 PM Post no. 23647 in reply to 20662

    Re: DataGrid How to use the Enter key to move to the first column after insert

    I used this solution for tabbing in V3.1 of the datagrid. Now, after upgrading to V3.2 this did not compile and it complains that grid.CurrentColumn is of type ColumnBase. I change Column to ColumnBase and it does compile and work.

    Now, I'm getting a bug report from a user that portions of the datagrid disappear after tabbing and I'm wondering if the change to ColumnBase has anything to do with the problem.

    Any thoughts?

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.