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.


Using System.Windows.Forms

and then entire code of DataGridview class will be

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);
           }
      }
   }

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.

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C#, CodeProject and tagged , , . Bookmark the permalink.

4 Responses to Prevent Focus at Particular Cell of DataGridView

  1. LeoCarla says:

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

  2. Pingback: Prevent Focus at Particular Cell of DataGridView-2 | RranjanK's Blogs

  3. Vadim Vinograd says:

    Excellent! It working! Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s