Help with controling changes in DataGridView rows


How do i allow users to only update(amend, delete) rows where values in country column is login country, for example countrylogin.text = USA, using VB.NET?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
If you feed the grid through your code, simply check the data for the country column when adding a row and set it's ReadOnly property when needed.

If you fill the row through data binding, then loop through the rows after it is full and mark as ReadOnly the rows that do not have the proper value in the country column.
vcharlesAuthor Commented:

Can you please send me an example of the second option. I am loading the grid  to a Dataset.

Jacques Bourgeois (James Burger)PresidentCommented:
Come on V. It's not as if you were a beginner. You've been at it for a while. Try to work it out by yourself once we've given you the basic technique. This is the best way to learn.

Explore the properties and methods of the objects that you use. This is what I did to answer your question. This is what most "experts" do when they confront a problem of their own or try to answer a question here.

If you look at the properties of the DataGridView, you see that it has a Rows property. Makes sense that it represents the rows in the grid.

When you have a lot of objects represented by a property whose name is plural, it is always a collection or an array. You know that a For loop enables you to move through a collection or an array.

If you look at the properties of a row, you see ReadOnly, this should jump in your face. And the tooltip that appears when you type it in the code window tells you that you use it to get or set the property, meaning that you can change it.

That is how any "expert" work half of the time to answer one of your questions. He is not sure of the answer. So he only creates a form, put a DataGridView in it and type DataGridView1. (with the dot). He then just follows the lead the lists of properties and methods given by IntelliSense give him, looking at the tooltips to get some information, hitting F1 if not sure.

It's not that we do not want to answer your questions, we would not be here if we did not. I would not have answered this one if I did not get something from helping programmers who have less experience than I have.

But if you would take the time to go through the simple technique I described here, you would solve your problems in a few minutes instead of waiting for us to finish a meal, going out for a walk (or a couple of hours the woodworking shop as is often the case for me). We would lose the points that you give us, but you would solve your problems in minutes instead of hours, and end up being very proud of yourself by finding the answer on your own.


            For Each row As DataGridViewRow In DataGridView1.Rows
                  If CStr(row.Cells("Country").Value) = countrylogin.text Then
                        row.ReadOnly = True
                  End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:

Thank You for your very good advice and   solution.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.