Hello, I am developing some software for a team and I am using the DataGridView and I want to have a button that changes the readonly on and off. I have tried DataGridView1.Readonly = False but that does not seem to work. Any suggestions? Thanks!
Printable View
Hello, I am developing some software for a team and I am using the DataGridView and I want to have a button that changes the readonly on and off. I have tried DataGridView1.Readonly = False but that does not seem to work. Any suggestions? Thanks!
This should work, it works for me DataGridView1.Readonly = False , will allow you to Edit.
You say that you want to turn the editability of the grid on and off but you only show code to turn it off. If you want to turn it on as well then of course that code won't work. If what you want is to toggle the ReadOnly column then you do this:The Not keyword negates a Boolean value, so that will set it to True if it's False and to False if it's True.vb.net Code:
DataGridView1.ReadOnly = Not DataGridView1.ReadOnly
So, to then reenable readonly, I would then just remove the not?
I did, it is just that the columns didn't let me edit them with the button
What I described works exactly as it should. If it's not working for you then, assuming nothing is corrupt on your machine, you did it wrong. If we don't know what you did then we don't know what's wrong with it. To test, I created a new project, added a Button and a DataGridView to the form and then added this code:
As I said, it worked exactly as expected.vb.net Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim table As New DataTable With table.Columns .Add("Name", GetType(String)) .Add("Number", GetType(Integer)) End With With table.Rows .Add("Peter", 1) .Add("Paul", 2) .Add("Mary", 3) End With DataGridView1.DataSource = table End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click DataGridView1.ReadOnly = Not DataGridView1.ReadOnly End Sub End Class
Alright I figured it out with the help of you. I figured out that I can not declare it all readonly in the properties but in the Formload event instead and now it all works perfectly. Thank you for teaching me new stuff!
It doesn't matter whether you the grid's ReadOnly property is True or False in the designer. At least, that's the case if the columns are created at run time, as they are in my example. I haven't tested with columns created at design time though. I wouldn't have thought that there would be a difference but maybe there is.