none
Datagridviewrow readonly not working RRS feed

  • Question

  • Hi,

    I have a datagridview bound to a collection class. Each Item in the collection equals a row in the datagridview. When the Item value = x the row needs to be read-only.. But I can't get it to work. I can still edit cells where I am not supposed to.

            dgvPurchaseOrderLines.DataSource = PurchaseOrder.Lines
    
            For Each oRow As DataGridViewRow In dgvPurchaseOrderLines.Rows
                Dim oPurchaseOrderLine As InfinityLib.PurchaseOrderLine = oRow.DataBoundItem
    
                If oPurchaseOrderLine.Status = InfinityLib.PurchaseOrderDeliveryStatusEnum.LinkedToStock Then
                    oRow.ReadOnly = True
                End If
    
            Next


    Tuesday, January 8, 2019 3:00 PM

Answers

  • Hello,

    In the following example (I took an existing project for this) I have a DataGridView where each row has a primary key and if the id is 1,3 or 5 make that row read-only.

    Dim readOnlyValues = {1, 3, 5}
    
    For Each row As DataGridViewRow In ProductsDataGridView.Rows
        If readOnlyValues.Contains(CInt(row.Cells(0).Value)) Then
            row.ReadOnly = True
        End If
    Next

    The container/data source does not matter e.g.

    Dim dtProducts = NorthWindAzure2DataSet.Tables("Products")
    For rowindex As Integer = 0 To dtProducts.Rows.Count - 1
        If dtProducts.Rows(rowindex).Field(Of Integer)("ProductId") > 2 Then
            ProductsDataGridView.Rows(rowindex).ReadOnly = True
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    My code block was placed in the constructor. When I create a sub with the Mybase.Load event it works fine.

    That makes sense, nothing like that should ever be placed in the constructor. My recommendation is not load event but the Shown event. Developers in general get use to using the load event because it's easy to create by double clicking the form while Shown is done via the drop down.

    The issue with code in load verses shown is that the load event is known to let runtime errors not be thrown while shown will throw exceptions.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by E_Jef Friday, January 11, 2019 12:59 PM
    Wednesday, January 9, 2019 9:59 AM
    Moderator
  • Hello,

    In the following example (I took an existing project for this) I have a DataGridView where each row has a primary key and if the id is 1,3 or 5 make that row read-only.

    Dim readOnlyValues = {1, 3, 5}
    
    For Each row As DataGridViewRow In ProductsDataGridView.Rows
        If readOnlyValues.Contains(CInt(row.Cells(0).Value)) Then
            row.ReadOnly = True
        End If
    Next

    The container/data source does not matter e.g.

    Dim dtProducts = NorthWindAzure2DataSet.Tables("Products")
    For rowindex As Integer = 0 To dtProducts.Rows.Count - 1
        If dtProducts.Rows(rowindex).Field(Of Integer)("ProductId") > 2 Then
            ProductsDataGridView.Rows(rowindex).ReadOnly = True
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    My code block was placed in the constructor. When I create a sub with the Mybase.Load event it works fine.
    • Marked as answer by E_Jef Friday, January 11, 2019 12:59 PM
    Wednesday, January 9, 2019 8:01 AM

All replies

  • Hello,

    In the following example (I took an existing project for this) I have a DataGridView where each row has a primary key and if the id is 1,3 or 5 make that row read-only.

    Dim readOnlyValues = {1, 3, 5}
    
    For Each row As DataGridViewRow In ProductsDataGridView.Rows
        If readOnlyValues.Contains(CInt(row.Cells(0).Value)) Then
            row.ReadOnly = True
        End If
    Next

    The container/data source does not matter e.g.

    Dim dtProducts = NorthWindAzure2DataSet.Tables("Products")
    For rowindex As Integer = 0 To dtProducts.Rows.Count - 1
        If dtProducts.Rows(rowindex).Field(Of Integer)("ProductId") > 2 Then
            ProductsDataGridView.Rows(rowindex).ReadOnly = True
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 8, 2019 3:25 PM
    Moderator
  • In order to check that this property has effect, try a simple loop like this:

    For Each oRow As DataGridViewRow In dgvPurchaseOrderLines.Rows

       oRow.ReadOnly = True

    Next

    If all of the rows become read-only, then maybe something is wrong with your If or Status property. Put a breakpoint inside If to check that it works as expected.


    • Edited by Viorel_MVP Tuesday, January 8, 2019 3:27 PM
    Tuesday, January 8, 2019 3:26 PM
  • My breakpoint hits. I checked. But I can still edit cells in the row.
    Tuesday, January 8, 2019 3:58 PM
  • Here is a conceptual example, I added the columns in the designer and set their DataPropertyName in the designer too. If Status is true disable the row, the reverse works too e.g. check if Status is false disable the row.

    Note I cast DataBoundItem to the proper type rather than letting the runtime figure it out for me.

    Public Class Form1
        Private _productsList As New List(Of Product) From
            {
                New Product() With {.Name = "AAA", .Status = True},
                New Product() With {.Name = "BBB", .Status = False},
                New Product() With {.Name = "CCC", .Status = True},
                New Product() With {.Name = "DDD", .Status = False}
            }
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = _productsList
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim prod As Product = CType(row.DataBoundItem, Product)
                If prod.Status = True Then
                    row.ReadOnly = True
                End If
            Next
        End Sub
    End Class
    Public Class Product
        Public Property Name() As String
        Public Property Status() As Boolean
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 8, 2019 5:22 PM
    Moderator
  • Hi,

    You can try the CellBeginEdit event,For example,  if you only want the row  which the gender is female to be not editable .

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim students As List(Of Student) = New List(Of Student)()
            Dim hat As Student = New Student("Hathaway", "12", "Male")
            Dim peter As Student = New Student("Peter", "14", "Male")
            Dim dell As Student = New Student("Dell", "16", "Male")
            Dim anne As Student = New Student("Anne", "19", "Female")
            students.Add(hat)
            students.Add(peter)
            students.Add(dell)
            students.Add(anne)
            Me.DataGridView1.DataSource = students
        End Sub
        Public Class Student
            Dim _name As String
            Public Property Name As String
                Get
                    Return _name
                End Get
                Set(ByVal value As String)
                    _name = value
                End Set
            End Property
            Dim _age As String
            Public Property Age As String
                Get
                    Return _age
                End Get
                Set(ByVal value As String)
                    _age = value
                End Set
            End Property
            Dim _gender As String
            Public Property Gender As String
                Get
                    Return _gender
                End Get
                Set(ByVal value As String)
                    _gender = value
                End Set
            End Property
            Public Sub New(Name As String, Age As String, Gender As String)
                Me._name = Name
                Me._age = Age
                Me._gender = Gender
            End Sub
    
        End Class
    
        Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
            If DataGridView1.Rows(e.RowIndex).Cells("Gender").Value.ToString = "Female" Then
                e.Cancel = True
            End If
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 9, 2019 2:30 AM
  • Hello,

    In the following example (I took an existing project for this) I have a DataGridView where each row has a primary key and if the id is 1,3 or 5 make that row read-only.

    Dim readOnlyValues = {1, 3, 5}
    
    For Each row As DataGridViewRow In ProductsDataGridView.Rows
        If readOnlyValues.Contains(CInt(row.Cells(0).Value)) Then
            row.ReadOnly = True
        End If
    Next

    The container/data source does not matter e.g.

    Dim dtProducts = NorthWindAzure2DataSet.Tables("Products")
    For rowindex As Integer = 0 To dtProducts.Rows.Count - 1
        If dtProducts.Rows(rowindex).Field(Of Integer)("ProductId") > 2 Then
            ProductsDataGridView.Rows(rowindex).ReadOnly = True
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    My code block was placed in the constructor. When I create a sub with the Mybase.Load event it works fine.
    • Marked as answer by E_Jef Friday, January 11, 2019 12:59 PM
    Wednesday, January 9, 2019 8:01 AM
  • Hello,

    In the following example (I took an existing project for this) I have a DataGridView where each row has a primary key and if the id is 1,3 or 5 make that row read-only.

    Dim readOnlyValues = {1, 3, 5}
    
    For Each row As DataGridViewRow In ProductsDataGridView.Rows
        If readOnlyValues.Contains(CInt(row.Cells(0).Value)) Then
            row.ReadOnly = True
        End If
    Next

    The container/data source does not matter e.g.

    Dim dtProducts = NorthWindAzure2DataSet.Tables("Products")
    For rowindex As Integer = 0 To dtProducts.Rows.Count - 1
        If dtProducts.Rows(rowindex).Field(Of Integer)("ProductId") > 2 Then
            ProductsDataGridView.Rows(rowindex).ReadOnly = True
        End If
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    My code block was placed in the constructor. When I create a sub with the Mybase.Load event it works fine.

    That makes sense, nothing like that should ever be placed in the constructor. My recommendation is not load event but the Shown event. Developers in general get use to using the load event because it's easy to create by double clicking the form while Shown is done via the drop down.

    The issue with code in load verses shown is that the load event is known to let runtime errors not be thrown while shown will throw exceptions.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by E_Jef Friday, January 11, 2019 12:59 PM
    Wednesday, January 9, 2019 9:59 AM
    Moderator
  • Hello,

    Can you please close this thread/question and mark any replies that helped as helpful and for the resolution seems since you found the solution mark yourself as answered which can assist others searching for the same or similar solution.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 10, 2019 11:59 AM
    Moderator