Results 1 to 9 of 9

Thread: DataGridView Readonly On/off Button?

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    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!

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: DataGridView Readonly On/off Button?

    This should work, it works for me DataGridView1.Readonly = False , will allow you to Edit.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. 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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: DataGridView Readonly On/off Button?

    So, to then reenable readonly, I would then just remove the not?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: DataGridView Readonly On/off Button?

    Quote Originally Posted by Dragnorian View Post
    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: DataGridView Readonly On/off Button?

    I did, it is just that the columns didn't let me edit them with the button

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Dim table As New DataTable
    5.  
    6.         With table.Columns
    7.             .Add("Name", GetType(String))
    8.             .Add("Number", GetType(Integer))
    9.         End With
    10.  
    11.         With table.Rows
    12.             .Add("Peter", 1)
    13.             .Add("Paul", 2)
    14.             .Add("Mary", 3)
    15.         End With
    16.  
    17.         DataGridView1.DataSource = table
    18.     End Sub
    19.  
    20.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    21.         DataGridView1.ReadOnly = Not DataGridView1.ReadOnly
    22.     End Sub
    23.  
    24. End Class
    As I said, it worked exactly as expected.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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
  •  



Click Here to Expand Forum to Full Width