-
Mar 7th, 2017, 03:23 PM
#1
Thread Starter
Lively Member
DataGridView Readonly On/off Button?
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!
-
Mar 7th, 2017, 03:36 PM
#2
Re: DataGridView Readonly On/off Button?
This should work, it works for me DataGridView1.Readonly = False , will allow you to Edit.
-
Mar 7th, 2017, 07:10 PM
#3
Re: DataGridView Readonly On/off Button?
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:
vb.net Code:
DataGridView1.ReadOnly = Not DataGridView1.ReadOnly
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.
-
Mar 7th, 2017, 09:58 PM
#4
Thread Starter
Lively Member
Re: DataGridView Readonly On/off Button?
So, to then reenable readonly, I would then just remove the not?
-
Mar 7th, 2017, 10:34 PM
#5
Re: DataGridView Readonly On/off Button?
Originally Posted by Dragnorian
So, to then reenable readonly, I would then just remove the not?
It's like you didn't even read what I posted. Read my previous post again and properly this time. Do what I told you to do, no more and no less.
-
Mar 7th, 2017, 11:05 PM
#6
Thread Starter
Lively Member
Re: DataGridView Readonly On/off Button?
I did, it is just that the columns didn't let me edit them with the button
-
Mar 7th, 2017, 11:35 PM
#7
Re: DataGridView Readonly On/off 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:
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
As I said, it worked exactly as expected.
-
Mar 8th, 2017, 10:32 PM
#8
Thread Starter
Lively Member
Re: DataGridView Readonly On/off Button?
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!
Last edited by Dragnorian; Mar 8th, 2017 at 10:38 PM.
Reason: Achievement Unlocked: Learned Something New
-
Mar 9th, 2017, 12:05 AM
#9
Re: DataGridView Readonly On/off Button?
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|