Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF application. I face a situation to add a windows form which contains a datagridview. I want to move focus to next cell in datagridview on pressing the enter key. In a windows application it is working using the ProcessDialogKey. But it is not working in WPF application
Posted

1 solution

I am not sure whether you are asking for the code for WPF or Windows forms now, this is for WPF the code can be edited slightly for a Windows form if you wish.

C#
private void dg_PreviewKeyDown(object sender, KeyEventArgs e)
{
    try
    {
        if (e.Key == Key.Enter)
        {
            e.Handled = true;
            var cell = GetCell(dgIssuance, dgIssuance.Items.Count - 1, 2);
            if (cell != null)
            {
                cell.IsSelected = true;
                cell.Focus();
                dg.BeginEdit();
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox(ex.Message, "Error", MessageType.Error);
    }
}  

public static DataGridCell GetCell(DataGrid dg, int row, int column)
{
    var rowContainer = GetRow(dg, row);

    if (rowContainer != null)
    {
        var presenter = GetVisualChild<datagridcellspresenter>(rowContainer);
        if (presenter != null)
        {
            // try to get the cell but it may possibly be virtualized
            var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
    }
    return null;
}</datagridcellspresenter>
 
Share this answer
 
v2
Comments
Naz_Firdouse 17-Jul-13 4:35am    
The issue in this code is we need to specify a particular row and column index to GetCell method.
I am able to get the selected row using

rowIndex = this.datagrid1.SelectedIndex;

but to get the column index within that row?
Any idea?
Naz_Firdouse 17-Jul-13 4:53am    
I got the solution to get the current column index
colIndex = this.datagrid1.CurrentColumn.DisplayIndex;
saritagovind 17-Jul-13 5:09am    
I need code for windows forms.. i created this windows form in my WPF application for a special use.
Rain Dancer 17-Jul-13 6:02am    
Saritagovind, you can use basically the same code for Windows forms as with WPF, changing some code slightly, but the same method is used. I basically gave you the answer. I am not being mean in saying this, but from here you should take the code and figure it out for yourself. There is no other way to learn, but to learn from your own mistakes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900