Click here to Skip to main content
12,349,363 members (27,377 online)
Click here to Skip to main content
Add your own
alternative version

Stats

59.2K views
5.8K downloads
49 bookmarked
Posted

Cheating the DataGridView

, 11 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

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:

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)

Share

About the Author

Samuel Cherinet
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThank you Pin
Member 406078529-May-15 1:24
memberMember 406078529-May-15 1:24 
QuestionAwesome .. Pin
Member 103761935-Dec-13 10:38
memberMember 103761935-Dec-13 10:38 
QuestionGreat Stuff Pin
Member 216395925-Jul-13 18:47
memberMember 216395925-Jul-13 18:47 
GeneralThanks Pin
Thahir Pattanathel8-Jul-12 1:13
memberThahir Pattanathel8-Jul-12 1:13 
GeneralThank you Pin
Rashmikant Kaneria11-Apr-12 17:53
memberRashmikant Kaneria11-Apr-12 17:53 
QuestionVery proper solution Pin
Rashmikant Kaneria11-Apr-12 17:52
memberRashmikant Kaneria11-Apr-12 17:52 
GeneralMy vote of 5 Pin
manoj kumar choubey27-Mar-12 21:30
membermanoj kumar choubey27-Mar-12 21:30 
QuestionThanks, but... Pin
brightak14-Feb-12 6:09
memberbrightak14-Feb-12 6:09 
AnswerRe: Thanks, but... Pin
rafael12466-Apr-12 6:01
memberrafael12466-Apr-12 6:01 
GeneralMy vote of 5 Pin
rafael124619-Dec-11 6:39
memberrafael124619-Dec-11 6:39 
QuestionThis control does not appear in the toolbox. Pin
rafael124612-Dec-11 5:18
memberrafael124612-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
samqty14-Dec-11 9:29
membersamqty14-Dec-11 9:29 
GeneralRe: This control does not appear in the toolbox. Pin
rafael124619-Dec-11 6:40
memberrafael124619-Dec-11 6:40 
SuggestionMy vote of 5 Pin
TheTriffidCollective8-Dec-11 1:38
memberTheTriffidCollective8-Dec-11 1:38 
GeneralMy vote of 5 Pin
Map_Tungtung5-Oct-11 20:49
memberMap_Tungtung5-Oct-11 20:49 
GeneralHow to write this in vb.net Pin
Alicealiciasawarak5-Jun-11 20:15
memberAlicealiciasawarak5-Jun-11 20:15 
GeneralSmall extension Pin
pavsej6-Apr-11 3:11
memberpavsej6-Apr-11 3:11 
GeneralThank You Pin
codesri26-Oct-09 15:17
membercodesri26-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 11 Mar 2009
Article Copyright 2009 by Samuel Cherinet
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid