Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Cheating the DataGridView

Rate me:
Please Sign up or sign in to vote.
4.57/5 (14 votes)
11 Mar 2009CPOL2 min read 93.3K   7.1K   52   18
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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank you Pin
Member 406078529-May-15 1:24
Member 406078529-May-15 1:24 
QuestionAwesome .. Pin
Member 103761935-Dec-13 10:38
Member 103761935-Dec-13 10:38 
QuestionGreat Stuff Pin
Member 216395925-Jul-13 18:47
Member 216395925-Jul-13 18:47 
GeneralThanks Pin
Thahir Pattanathel8-Jul-12 1:13
Thahir Pattanathel8-Jul-12 1:13 
GeneralThank you Pin
Rashmikant Kaneria11-Apr-12 17:53
Rashmikant Kaneria11-Apr-12 17:53 
QuestionVery proper solution Pin
Rashmikant Kaneria11-Apr-12 17:52
Rashmikant Kaneria11-Apr-12 17:52 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey27-Mar-12 21:30
professionalManoj Kumar Choubey27-Mar-12 21:30 
QuestionThanks, but... Pin
brightak14-Feb-12 6:09
brightak14-Feb-12 6:09 
AnswerRe: Thanks, but... Pin
rafael12466-Apr-12 6:01
rafael12466-Apr-12 6:01 
GeneralMy vote of 5 Pin
rafael124619-Dec-11 6:39
rafael124619-Dec-11 6:39 
QuestionThis control does not appear in the toolbox. Pin
rafael124612-Dec-11 5:18
rafael124612-Dec-11 5:18 
I am using VS2008, I download this assembly and reference it as mencioned in this article, but it does not appear in the toolbox. Am I missing a steep?. Any help will be usefull.

Thanks in advance.
AnswerRe: This control does not appear in the toolbox. Pin
Samuel Cherinet14-Dec-11 9:29
Samuel Cherinet14-Dec-11 9:29 
GeneralRe: This control does not appear in the toolbox. Pin
rafael124619-Dec-11 6:40
rafael124619-Dec-11 6:40 
SuggestionMy vote of 5 Pin
TheTriffidCollective8-Dec-11 1:38
TheTriffidCollective8-Dec-11 1:38 
GeneralMy vote of 5 Pin
Map_Tungtung5-Oct-11 20:49
Map_Tungtung5-Oct-11 20:49 
GeneralHow to write this in vb.net Pin
Alicealiciasawarak5-Jun-11 20:15
Alicealiciasawarak5-Jun-11 20:15 
GeneralSmall extension Pin
pavsej6-Apr-11 3:11
pavsej6-Apr-11 3:11 
GeneralThank You Pin
codesri26-Oct-09 15:17
codesri26-Oct-09 15:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.