Is it possible to make Rows in a DataGridView read only upon an Edit

Posted on 2011-02-28
Last Modified: 2013-11-26
I have a datagrid that is editable. I want to lock all rows in the grid when a person edits a row so that they can only change the row that they are editing. This way they can only change one row in the grid and be forced to save before changing anything else.

Does anyone have any pointers on how to do this?
Question by:imonfireDAMMIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 17

Expert Comment

ID: 34998338
unless you have asked your question wrongly, the functionality that you ask for ships with the datagridview. You literally can NOT edit 2 cells at any one time, let alone two rows!
LVL 16

Expert Comment

ID: 34998378
Instead of forcing them to save the changes from one row before editing another one why don't you set your grid to automatically save on row exit. You could add a messagebox if the user can change his/her mind at the last minute asking them if they really want to save it. If yes save changes if no cancel save changes.

Author Comment

ID: 34998389
OK let me re-ask this...

Once a row has been edited i want all the other rows to be locked so they cant edit them.

So if they click on another row, that row is read only...
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.


Author Comment

ID: 34998435
Im making a Wizard and that dataset associated with the datagrid  will not be saved till the end, it gives them a chance to cancel all changes. The wizard requires them to select and edit a row from a project but I do not want them changing two projects at once. They should know that but you know users, I'll get a guy changing stuff everywhere and wonder why his data is all screwed up. I try to account for all the things the users are going to try to do, so that my job is easier in the end :-)
LVL 16

Expert Comment

ID: 34998543
Then yes you can lock the other rows by iterating through all rows changing them to readonly except for the current row.
LVL 16

Accepted Solution

13598 earned 500 total points
ID: 34998655
Depending on the rest of your code you could put something like this either on your rowleave or rowvalidating event:
  If Me.DataGridView.IsCurrentRowDirty then
For Each myRow As DataGridViewRow In Me.DataGridView.Rows
 If myRow.Index <> e.RowIndex Then
                    myRow.ReadOnly = True
                End If
end if

Author Comment

ID: 34998844
ok THANK YOU! I was just about to ask which event to use. I think it would probably be better in the row leave. I will apply this and let you know how it works. It seems right. I always have a hard time figuring out which event to use...there are so many

Author Closing Comment

ID: 34999197
Thank you!!! You saved me a lot of work. My method that I was trying to implement was pretty lengthy and rediculous compared to this :-)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question