Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I encountered a strange behavior with the following code on a datagridview in VB2010. dgv is the name of the datagridview. In FormLoad, I wrote
VB
Private Sub frmdataLog_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    dgv.ColumnCount = 5
    dgv.Columns(0).Width = 27
    Dim j As Integer
    For j = 1 To 4
        dgv.Columns(j).Width = 85
    Next

    dgv.Rows.Add(1)
    dgv.Columns(0).HeaderText = "Sl No."
    dgv.Columns(1).HeaderText = "Test Reading Nm"
    dgv.Columns(2).HeaderText = "Standard Reading Nm"
    dgv.Columns(3).HeaderText = "Error in Nm"
    dgv.Columns(4).HeaderText = "% Uncertainty"
    For j = 0 To 5
        dgv.Columns(j).ReadOnly = True
        dgv.Columns(j).SortMode = DataGridViewColumnSortMode.NotSortable
    Next
    dgv.Columns(1).ReadOnly = False

End Sub

I am expecting column(1) to be editable while all other columns will be uneditable.

But I found that even column(1) is also readonly!

But when I added the following code, the problem got solved:
VB
Private Sub dgv_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEnter
    colVal = e.ColumnIndex
    rowVal = e.RowIndex
    If dgv.Columns(1).ReadOnly = True Then
        dgv.Columns(1).ReadOnly = False
    End If
End Sub

My question is why does the line dgv.Columns(1).ReadOnly = False not execute under Form load?
Posted
Updated 28-Jul-15 9:23am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900