Click here to Skip to main content
Click here to Skip to main content

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
Follow on   Google+

Comments and Discussions

 
QuestionAwesome .. PinmemberMember 103761935-Dec-13 10:38 
QuestionGreat Stuff PinmemberMember 216395925-Jul-13 18:47 
GeneralThanks PinmemberThahir Pattanathel8-Jul-12 1:13 
GeneralThank you PinmemberRashmikant Kaneria11-Apr-12 17:53 
QuestionVery proper solution PinmemberRashmikant Kaneria11-Apr-12 17:52 
GeneralMy vote of 5 Pinmembermanoj kumar choubey27-Mar-12 21:30 
QuestionThanks, but... Pinmemberbrightak14-Feb-12 6:09 
AnswerRe: Thanks, but... Pinmemberrafael12466-Apr-12 6:01 
GeneralMy vote of 5 Pinmemberrafael124619-Dec-11 6:39 
QuestionThis control does not appear in the toolbox. Pinmemberrafael124612-Dec-11 5:18 
AnswerRe: This control does not appear in the toolbox. Pinmembersamqty14-Dec-11 9:29 
GeneralRe: This control does not appear in the toolbox. Pinmemberrafael124619-Dec-11 6:40 
SuggestionMy vote of 5 PinmemberTheTriffidCollective8-Dec-11 1:38 
GeneralMy vote of 5 PinmemberMap_Tungtung5-Oct-11 20:49 
GeneralHow to write this in vb.net PinmemberAlicealiciasawarak5-Jun-11 20:15 
GeneralSmall extension Pinmemberpavsej6-Apr-11 3:11 
GeneralThank You Pinmembercodesri26-Oct-09 15:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.141022.1 | Last Updated 11 Mar 2009
Article Copyright 2009 by Samuel Cherinet
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid