vb.netdatagridviewdatagridviewcolumn

How to make a specific row in one column editable in datagridview when cellcontentclick in edit column in vb.net


I try it has not succeeded if I uncomment for the code DataGridView2.Columns("Qty").ReadOnly = True then it can be editable but all rows in one column but I want to be editable in the row I select after I click edit and if I move another row then it is readonly Please Guide me

Thanks

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 DataGridView2.DataSource = itrservice.GetLoadStocksoutdetailMaster()
        DataGridView2.Columns("No").ReadOnly = True
        DataGridView2.Columns("Invnop").ReadOnly = True
        DataGridView2.Columns("CodeProduct").ReadOnly = True
        DataGridView2.Columns("Qty").ReadOnly = True
    End Sub
 Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
 If e.ColumnIndex = 4 Then
                If DataGridView2.SelectedRows.Count = 0 Then
                    Return
                End If
            DataGridView2.Columns("Qty").ReadOnly = False

        End If
    End Sub

view datagridview

view datagridview


Solution

  • The DataGridViewCell inherits the ReadOnly property from its owning column or row. Setting the property to True means you can't use the cell's EditingControl to change the value and it's only can be changed by code.

    You can instead set the DataGridView.EditMode property to DataGridViewEditMode.EditProgrammatically to disable putting the cells in edit mode by the mouse and key inputs. By code in the CellContentClick event handler, select and put a given cell in edit mode when you click the link cell of the selected row.

    Example to put the Qty cell in edit mode.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView2.DataSource = itrservice.GetLoadStocksoutdetailMaster()
        DataGridView2.EditMode = DataGridViewEditMode.EditProgrammatically
    End Sub
    
    Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
        If e.ColumnIndex = 4 Then
            DataGridView2.CurrentCell = DataGridView2(3, e.RowIndex)
            DataGridView2.BeginEdit(True)
        End If
    End Sub
    

    You can use the columns Name property to identify them instead of the index..

    Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
        If DataGridView2.Columns(e.ColumnIndex) Is DataGridView2.Columns("coledit") Then
            DataGridView2.CurrentCell = DataGridView2("Qty", e.RowIndex)
            DataGridView2.BeginEdit(True)
        End If
    End Sub
    

    Another example on how you should populate the grid and add the DataGridViewLinkColumn.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        DataGridView2.EditMode = DataGridViewEditMode.EditProgrammatically
        DataGridView2.DataSource = itrservice.GetLoadStocksoutdetailMaster().ToList()
        DataGridView2.Columns.Add(New DataGridViewLinkColumn With {
            .Name = "coledit",
            .Text = "Edit",
            .HeaderText = "",
            .UseColumnTextForLinkValue = True,
            .TrackVisitedState = False
        })
        ' Optional...
        DataGridView2.Columns("coledit").DefaultCellStyle.SelectionBackColor = Grid.Columns("coledit").DefaultCellStyle.BackColor
    End Sub
    

    Side note, IMO the FullRowSelect is not appropriate SelectionMode here. Use the default RowHeaderSelect value instead.