Solved

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

Posted on 2011-02-28
8
567 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

14 Experts available now in Live!

Get 1:1 Help Now