How to make DataGridView Columns read only

I have a WinForm that has a DataGridView.  When the form opens I want to enable/diable certain fields based on who is logged in, so in the form's load event I am trying to disable editing of certain fields.  The below two statements give me the following errror:  NullReferenceException occurred.  Object reference not set to an instance of an object.  Is there any difference for a DataGrid Please advise what I need to do to resolve.  thanks.

            Me.DataGridView1.Columns.Item("FieldName").Visible = True
            Me.DataGridView1.Columns.Item("FieldName").ReadOnly = True
KentDBerryAsked:
Who is Participating?
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.

jacieslaCommented:
Datagridview1.Columns(3).ReadOnly = True

This will make the 3rd column read only. Let me know if this works for you. If not please give me the specific error you receive.
0

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
vb_jonasCommented:
DataGridView1.Columns("FieldName").ReadOnly
0
Jorge PaulinoIT Pro/DeveloperCommented:
DataGridView1.Columns("ColumnName").ReadOnly = True
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

KentDBerryAuthor Commented:
DataGridView1.Columns("FieldName").ReadOnly  gives the same error as does
DataGridView1.Columns("ColumnName").ReadOnly = True

jaciesla's below code works but I need to reference the column by its actual text name like "FieldName".  jaciesla, how would you do that?
Datagridview1.Columns(3).ReadOnly = True works
0
Jorge PaulinoIT Pro/DeveloperCommented:
DataGridView1.Columns("ColumnName").ReadOnly = True works like Datagridview1.Columns(3).ReadOnly = True

The problem is that you must know the column name that is could by different from the column text. You can do this to get the right name:

MessageBox.Show(Datagridview1.Columns(3).Name)
0
Jorge PaulinoIT Pro/DeveloperCommented:
You an use the index (as integer) or the name (as string)
0
jacieslaCommented:
The code provided by others ie. DataGridView1.Columns("ColumnName").ReadOnly = True
should work the same as mine. I just prefer to reference my columns numerically. Is it possible that the name of the column is not spelled correctly? That is the only thing I can think of that would cause problems.
Thanks,
James
0
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
.NET Programming

From novice to tech pro — start learning today.