.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Prevent Focus at Particular Cell of DataGridView

Posted By:Ravi Ranjan Kumar       Posted Date: July 30, 2011    Points: 200    Category: Windows Application    URL: http://www.dotnetspark.com  

This article enable you to learn How to prevent focus on particular cell of Datagridview using C#
 

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.



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)

when you press Add button it will create a new class in your Program under Solution Explorer 
so after it you need to inherit DataGridView class with current class. 
take a look how 


and DataGridView class is accessible only when you've declared 
Windows.Form namespace like 
Using System.Windows.Forms 
and entire code will be of myDataGridView Class is 

 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 class 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.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend