Prevent Focus at Particular Cell of DataGridView

Introduction

Some time we need to create an application in which Datagridview doesn’t allow to give functionality to user to put focus on a particular cell. its a very needed requirement which we face time to time. suppose a user have a value and he doesn’t want to make it editable, and want to prevent it from any focus so in this case this article might help you

You need to Know

like every control DataGridView have some by default value like DataGridView navigation model does not have any ability to restrict focus to a specific cell.

To implement functionality to give accesss user to prevent cell from focus of Users. we need to implement our own navigation logic by overriding the appropriate keyboard, navigation and mouse methods such as DataGridView.OnKeyDown, DataGridView.ProcessDataGridViewKey,DataGridView.SetCurrentCellAddressCore, DataGridView.OnMouseDown

Lets an example if you want to prevent user to give focus on second column of DataGridView. then in this case you need to drive DataGridView Class and Override the SetCurrentCellAddressCore and SetSelectedCellCore to accomplish the logic you want to perform.

How it Will Look

you can see user is able to focus on almost every column except 1st(NationalIDNumber) Column of DataGridView control.

Animated Image - Preventing Cell of DataGridView

How to Do

Take a look at given images and code to learn how to perform it.
Its better to create an different class and for it you need to perform following steps as shown in Given figure.
right Click on Project name under solution Explorer and then Select Add and then click on Class as shown below

It will AddNewItem Form where you can select class and then need to name it(class)

Now Select Class from Visual C# Items and enter the Name (Example MyDataGridView.cs)  in textbox and then press Add button.
when you press Add button it will create a new class in your Program under Solution Explorer so after creating class we need to inherit DataGridView class with current class (MyDataGridView).
take a look how

and DataGridView class is accessible only when you’ve declared
Windows.Form namespace like.
Note :- To access DataGridView class you need to include System.Window.Form namespace in your program.

[sourcecode language=”csharp”]

Using System.Windows.Forms

[/sourcecode]

and then entire code of DataGridview class will be

[sourcecode language=”csharp”] public class myDataGridView : DataGridView
{
private int columnToSkip = -1;
public int ColumnToSkip
{
get { return columnToSkip; }
set { columnToSkip = value; }
}

protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex,
bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)
{

if (columnIndex == this.columnToSkip && this.columnToSkip != -1)
{
if (this.columnToSkip == this.ColumnCount – 1)
{
return base.SetCurrentCellAddressCore(0, rowIndex + 1,
setAnchorCellAddress, validateCurrentCell, throughMouseClick);
}
else
{
if (this.ColumnCount != 0)
{
return base.SetCurrentCellAddressCore(columnIndex + 1, rowIndex,
setAnchorCellAddress, validateCurrentCell, throughMouseClick);
}
}
}
return base.SetCurrentCellAddressCore(columnIndex, rowIndex,
setAnchorCellAddress, validateCurrentCell, throughMouseClick);
}

protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
{
if (columnIndex == this.columnToSkip)
{
if (this.columnToSkip == this.ColumnCount – 1)
{
base.SetSelectedCellCore(0, rowIndex + 1, selected);
}
else
{
if (this.ColumnCount != 0)
{
base.SetSelectedCellCore(columnIndex + 1, rowIndex, selected);
}
}
}
else
{
base.SetSelectedCellCore(columnIndex, rowIndex, selected);
}
}
}
[/sourcecode]

when collapse each and every function of class. program will look like as given below

Now Compile application once time to came existence of myDataGridView control in toolbox. after one successful execution it will showing in Toolbox as given below and then we can drag it from toolbox to Form and can use it with some additional functionality

Now you need to write query to ” bind DataGridView with your database ” as give below
here I am using AdventureWorks database to bind DataGridView
you can see at last two circled line in which 1st line is responsible to bind DataGridview to datasource and very last line is responsible for skipping value of 1st column of DataGridView
Take a look

Now Press F5 or Compile it to see Effect
you will notice every columns is accessible Except 1st Column which is bounded with red Circled.

A snap of of compiled program

So Finally we learned how we can prevent to focusing on particular cell of DataGridView 🙂

To get more practical view you can download source code also.
Click Me!!   to download Source code.

4 thoughts on “Prevent Focus at Particular Cell of DataGridView”

  1. Vadim Vinograd says:

    Excellent! It working! Thanks.

  2. Pingback: Prevent Focus at Particular Cell of DataGridView-2 | RranjanK's Blogs
  3. Trackback: Prevent Focus at Particular Cell of DataGridView-2 | RranjanK's Blogs
  4. LeoCarla says:

    It works fine but looses focus while editing or adding new row. how to solve it?

    1. RaviRanjanKr says:

      hey! LeoCarla my new article-[ http://raviranjankr.wordpress.com/2011/12/15/prevent-focus-at-particular-cell-of-datagridview-2/ ] might help you to get answer of your query.

Leave a Reply