1

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

roy
  • 693
  • 2
  • 11

1 Answers1

1

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.

dr.null
  • 4,032
  • 3
  • 9
  • 12
  • Thanks your reply, I've tried from your code and from your step2 but it hasn't worked in me. Could there be something wrong in me – roy Aug 25 '23 at 09:25
  • @pret Don't set the `ReadOnly` properties to `True`. It must set to `False`. – dr.null Aug 25 '23 at 09:28
  • okay I set from bound column properties and for `Coledit` I set readyonly to false too – roy Aug 25 '23 at 09:41
  • I've tried it anyway I can't edit if there is something wrong in my DataGridView – roy Aug 25 '23 at 12:54
  • @pret What happens when you use this code? And, how do you add the link column? After setting the grid's `DataSource` property? What does this `GetLoadStocksoutdetailMaster()` return? Change it to `DataGridView2.DataSource = itrservice.GetLoadStocksoutdetailMaster().ToList()`, double check your code, and try. On other hand, if you want to make the entire selected row editable, then this is another story. So, what exactly do you need here? What do you want to happen when you click the link? – dr.null Aug 25 '23 at 13:37
  • I encountered a problem , if i make it like this works editable `If e.ColumnIndex = 0 Then DataGridView1.CurrentCell = DataGridView1(4, e.RowIndex) DataGridView1.BeginEdit(True) End If` but Why is my Edit Link column to 0 but the DataGridView position to Column 4 – roy Aug 25 '23 at 13:39
  • I add directly to the datagridview with the header name and for datapropertyname it must be the same as the model class but for column link i did not fill in datapropertyname and headername also I made blank and only the deisgn name became "coledit" – roy Aug 25 '23 at 13:48
  • `You can use the columns Name property to identify them instead of the index..` Your solution works perfectly and for this `Another example on how you should populate the grid and add the DataGridViewLinkColumn.` This solution from you goes perfectly if I run the first answer from you. Why if adding directly in the DataGridView without going through the code changes the position of the index column? – roy Aug 25 '23 at 14:21
  • @pret You didn't post that part of code (adding the link column) to tell you why. Did you play with the columns `DisplayIndex` property? Did you add the link column first? It depends. – dr.null Aug 25 '23 at 14:27
  • `You didn't post that part of code (adding the link column) to tell you why` For this I added directly from the DataGridView column without going through the code . ` Did you play with the columns DisplayIndex property?` No , `Did you add the link column first? It depends` The position of the link column is in the last position of the DataGridView – roy Aug 25 '23 at 14:31
  • @pret Check the link column position in the grids `Columns` collection and its `DisplayIndex` property. You need to visit the grid's documentation and use it a lot to master it. It's a complex nested control. – dr.null Aug 25 '23 at 14:37
  • I have another post maybe you can give me a guide . [link](https://stackoverflow.com/questions/76954293/how-to-create-a-user-role-based-permissions-for-each-form-in-dapper-with-access?noredirect=1#comment135673930_76954293) – roy Aug 25 '23 at 15:15
  • Why did you remove your answer from this [link](https://stackoverflow.com/questions/76975666/why-looping-each-row-in-a-datagridview-doesnt-show-all-value-in-the-messagebox?noredirect=1#comment135709399_76975666) , sorry if I offended you. Because I want to mark your answer – roy Aug 28 '23 at 08:40