7
votes

I'm trying to develop a wpf app with a datagrid, and i want to allow the users to enter values like in excel.

Example: The datagrid has 2 columns, Name and BarCode The user is editing the BarCode on the first row, and when the user press Enter key, the focus should move the the row below on the BarCode cell.

The user must be able to use a barcode scanner to register the barcodes, on the existing products list, without need to user the mouse or the keyboard.

Any ideas on how to implement this behavior?

Thanks, Frederico

4
You want the datagrid to handle the Enter Key like the Tab Key?Ahmad Hammoud
No, The tab key sets the focus to the next cell, i need the focus to be set on the same column, but in the row below of the current. Like in excel, when the user is editing a cell, when he press enter the cell below receives focus and start edit.Frederico Regateiro
But it's already like excel.. can u explain more?Ahmad Hammoud
my problem is to move and edit the the cell below, after hit enterFrederico Regateiro

4 Answers

5
votes

A simpler solution is capture the KeyDown event and if the key is 'Enter', then move to the next tab.

private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var u = e.OriginalSource as UIElement;
    if (e.Key == Key.Enter && u != null)
    {
        e.Handled = true;
        u.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
    }
}
2
votes

I've just done this with some extra code, by handle the PreviewKeyUp event on the Datagrid, the working code is:

private void DataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.Enter) || (e.Key == Key.Return))
    {
        DataGrid grid = sender as DataGrid;
        if (grid.CurrentColumn.Header.ToString().Equals("Barcode", StringComparison.OrdinalIgnoreCase))
        {
            if (grid.SelectionUnit == DataGridSelectionUnit.Cell || grid.SelectionUnit == DataGridSelectionUnit.CellOrRowHeader)
            {
                var focusedElement = Keyboard.FocusedElement as UIElement;
                focusedElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
             }

             grid.BeginEdit();
             e.Handled = true;
        }
    }
}

The Column that hold the Barcode property is the only one i need for this behavior.

0
votes

Something like this ?

//MainWindow.xaml.cs

namespace BarcodeImplementationInDG
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Products> lst = new List<Products>();
        public MainWindow()
        {
            InitializeComponent();
            dg.ItemsSource = lst;
        }
    }
    public class Products
    {
        public string Product { get; set; }
        public string Barcode { get; set; }
    }
}

//MainWindow.xaml

<Window x:Class="BarcodeImplementationInDG.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dg" VerticalAlignment="Top" HorizontalAlignment="Left" CanUserAddRows="True" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Height="309" Width="507" >
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="ProductColumn" Binding="{Binding Product}" Header="Product"  />
                <DataGridTextColumn x:Name="BarcodeColumn" Binding="{Binding Barcode}" Header="Barcode"  />
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

The Data will be stored in the List lst, in case you want to use them

On Enter, The focus automatically moves to the second row..

0
votes

If 3rd party WPF Grids are acceptable, Essential Grid can be used as it has this Excel like behavior built into the Grid.

The whole suite of controls is available for free through the community license program if you qualify. Note: I work for Syncfusion.