Solved

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

Posted on 2011-02-28
8
573 Views
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?
0
Comment
Question by:imonfireDAMMIT
  • 4
  • 3
8 Comments
 
LVL 17

Expert Comment

by:nepaluz
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!
0
 
LVL 16

Expert Comment

by:13598
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.
0
 
LVL 1

Author Comment

by:imonfireDAMMIT
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...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:imonfireDAMMIT
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 :-)
0
 
LVL 16

Expert Comment

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

Accepted Solution

by:
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
next
end if
0
 
LVL 1

Author Comment

by:imonfireDAMMIT
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
0
 
LVL 1

Author Closing Comment

by:imonfireDAMMIT
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 :-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now