Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Cheating the DataGridView

4.57/5 (14 votes)
11 Mar 2009CPOL2 min read 95K   7.1K  
DataGridView modifications to enble the user to move horizontally through the cells by pressing the Enter key.

DemoApplication

Introduction

Displaying data directly from the data sources with a readable format readable is one of the benefits of using a grid view among others. Its quick implementation and easy connection to data sources makes it an ideal solution for beginner programmers. But my honeymoon with DataGridView ended when I advanced its purpose to entering data.

Since it look and feel is ideal for entering data, I attempted to use it to enter data using the cells for each item of data. When, what, and which event is triggered? All these things got me confused. Well, it took me quite a while, but I finally got the hang of it, but one of the things I could not go around was its behavior of vertical focus movement.

Once a user enters a data in to a cell and presses “Enter”, the focus goes to the next cell in the same column, vertically. In my case, in most of the situations, I wanted to enter data in a single row, one cell at a time. So driven by the fuel of looking for a solution, I wrote a very small code that will perform this task.

Using the code

I tried most of the possibilities to handle the key and focus events of the current cell to perform this task, they ended up wasting my time and frustrating me. Finally, I decided to override the functions that process DataGridView keys. In order for me to override these methods and for this component to be used any time, I extended the normal DataGridView using the code, to create a custom control.

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ZeeUIUtility
{
  //the class defination that inherits from DataGridView
  public class DataGridViewEx : DataGridView
 {}

The other thing about the grid view is, the focus moves with the navigation keys (Left, Right, Up, Down). What I did was override this behavior to make it think that the user has pressed the navigation keys whenever I wanted, and that is the basis of this solution. As in the code shown, override "ProcessDialogKey" and whenever the user presses Enter, call the method that moves the focus to the next cell.

C#
protected override bool ProcessDialogKey(Keys keyData)
{
    //if the key pressed is "return" then tell 
    //the datagridview to move to the next cell
    if (keyData == Keys.Enter)
    {
        MoveToNextCell();
        return true;
    }
    else
    return base.ProcessDialogKey(keyData);
}

The code for the move to the next cell goes like this:

C#
public void MoveToNextCell()
{
    int CurrentColumn, CurrentRow;

    //get the current indicies of the cell
    CurrentColumn = this.CurrentCell.ColumnIndex;
    CurrentRow = this.CurrentCell.RowIndex;

    //if cell is at the end move it to the first cell of the next row
    //other with move it to the next cell
    if (CurrentColumn == this.Columns.Count - 1 && 
        CurrentRow != this.Rows.Count - 1)
    {
        base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home));
        base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Down));
    }
    else
        base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Right));

Moving to the next cell means pressing the "right" key so that is what this code makes the DataGridView think. Furthermore, I have added code so that when the user reaches the end of the grid, the focus goes to the first cell of the next row, simulated by the keys "Home", then "Down", because that is what you basically do if you wish to move to the next cell in line.

Implementation

To use this control, all that is needed to be done is to download the assembly from this article and reference it. You will find the control in the toolbox. Since the rest of the properties of this control are same as that of the DataGridView, it will be easier to work with.

License

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