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
4341
Excel like functionality in a xamDataGrid
posted

Hello Support, 

We are currently working on providing excel like functionality in a xamDataGrid, i.e, navigating within the grid using the arrow keys, the tab key and the enter key. This was possible by setting the property CellClickAction="SelectCell".

However, we also require a cell's value to be edited without double clicking the cell or pressing the F2 key; and navigating to another cell using the navigation keys. We did find a work around for this by use of the following code.

 

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

        Try

            Dim currentCell As Cell = DirectCast(sender, XamDataGrid).ActiveCell

            If (e.Key = Key.Enter Or e.Key = Key.Return) Then

                CType(sender, XamDataGrid).ExecuteCommand(DataPresenterCommands.EndEditModeAndAcceptChanges)

                CType(sender, XamDataGrid).ExecuteCommand(DataPresenterCommands.CellBelow)

            End If

            If (currentCell Is Nothing OrElse currentCell.Field.Settings.AllowEdit = False) Then

                Exit Sub

            End If

                       keyPressed = e.Key

            If (previousFieldName <> currentCell.Field.Name Or previousRowNo <> currentCell.Record.Index Or keyPressed = Key.Escape) Then

                previousFieldName = currentCell.Field.Name

                previousRowNo = currentCell.Record.Index

                firstKeyStroke = True

                decimalValue = ""

            End If

            If (currentCell.IsInEditMode = False And keyPressed = Key.Space) Then

                If (firstKeyStroke = True) Then

                    firstKeyStroke = False

                    currentCell.Value = " "

                Else

                    currentCell.Value = currentCell.Value & " "

                End If

            ElseIf (currentCell.IsInEditMode = False And keyPressed = Key.Delete) Then

                If (firstKeyStroke = True) Then

                    firstKeyStroke = False

                    currentCell.Value = ""

                End If

            ElseIf (currentCell.IsInEditMode = True) Then

                If (keyPressed = Key.Up Or keyPressed = Key.Down) Then

                    e.Handled = True

                End If

            End If

        Catch ex As Exception

        End Try

    End Sub



    Private Sub xamGrid_PreviewTextInput(ByVal sender As System.Object, ByVal e As System.Windows.Input.TextCompositionEventArgs)

        Try

            Dim currentCell As Cell = DirectCast(sender, XamDataGrid).ActiveCell

            If (currentCell Is Nothing OrElse currentCell.Field.Settings.AllowEdit = False) Then

                Exit Sub

            End If

            If (currentCell.IsInEditMode = False And (keyPressed <> Key.Enter Or keyPressed <> Key.Return)) Then

                If (firstKeyStroke = True) Then

                    firstKeyStroke = False

                    If (keyPressed = Key.Back) Then

                        currentCell.Value = ""

                    ElseIf (currentCell.Field.DataType.FullName = "System.Decimal") Then

                        If ((keyPressed = Key.Decimal Or keyPressed = Key.OemPeriod) And Not decimalValue.Contains(".")) Then

                            decimalValue = ".0"

                            currentCell.Value = Convert.ToDecimal(decimalValue)

                            decimalValue = "."

                        ElseIf (IsNumeric(e.Text)) Then

                            decimalValue = e.Text

                            currentCell.Value = Convert.ToDecimal(decimalValue)

                        End If

                    Else

                        currentCell.Value = e.Text

                    End If

                Else

                    If (keyPressed = Key.Back) Then

                        If (currentCell.Value.ToString().Length <> 0) Then

                            currentCell.Value = currentCell.Value.ToString().Substring(0, currentCell.Value.ToString().Length - 1)

                        End If

                    ElseIf (currentCell.Field.DataType.FullName = "System.Decimal") Then

                        If ((keyPressed <> Key.Decimal And keyPressed <> Key.OemPeriod) And IsNumeric(e.Text) And _

                            decimalValue.Length < 10) Then

                            decimalValue = decimalValue & e.Text

                        End If


                        If ((keyPressed = Key.Decimal Or keyPressed = Key.OemPeriod) And Not decimalValue.Contains(".")) Then

                            decimalValue = decimalValue & ".0"

                            currentCell.Value = Convert.ToDecimal(decimalValue)

                            decimalValue = decimalValue.TrimEnd("0")

                        ElseIf (decimalValue <> "") Then

                            currentCell.Value = Convert.ToDecimal(decimalValue)

                        End If

                    Else

                        currentCell.Value = currentCell.Value & e.Text

                    End If

                End If

            End If

        Catch ex As Exception

        End Try

    End Sub

 

 

By using the above code, editing the existing records was possible, i.e. the RecordUpdating event was fired. But when trying to enter into a add new record (i.e. the AllowAddNew="True"), the RecordUpdating event does not fire, and hence the record never gets added.

We also noticed that CellUpdating and CellUpdated events are called on every text input using the above code, which is not required as these events contain validation; and every text input makes the validation fail, as this should happen only before and after the cell is updated and not during text input.

We have also attached a video for your reference.

Waiting for your reply.

Thank You.