在只读DGV中聚焦并进入特定单元格的编辑



我试图在ContextMenuStrip中为我的DataGridView做一个选项,这将允许在所选行中编辑特定的单元格,而DataGridViewReadOnly作为默认值。

我有下面的代码,似乎对其他人工作,但不适合我:

Private Sub Edit_ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Edit_ToolStripMenuItem.Click
If DGV_1.SelectedRows.Count > 1 Then
MsgBox("Please select only one item.")
Else
DGV_2.CurrentRow.Cells(2).ReadOnly = False
DGV_2.CurrentCell = DGV_2.Item("Qty", DGV_2.CurrentRow.Index)
DGV_2.BeginEdit(True)
End If
End Sub

更多上下文-DGV_2值基于DGV_1中的选择,因此If语句只允许在DGV_2中编辑Qty行单元格,当其值仅来自DGV_1中的1个选定行。

当单击此选项时,我希望用户在DGV_2的选定行内聚焦/进入Qty单元格的编辑模式。

同样,根据@jmcilhinney在最初问题下的评论,我能够通过制作DGV_2.ReadOnly = False然后配置以下事件来解决我的问题:

Private Sub DGV_2_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DGV_2.RowsAdded
If DGV_2.Rows.Count > 0 Then
DGV_2.Rows(e.RowIndex).Cells(0).ReadOnly = True
DGV_2.Rows(e.RowIndex).Cells(1).ReadOnly = True
DGV_2.Rows(e.RowIndex).Cells(2).ReadOnly = True
DGV_2.Rows(e.RowIndex).Cells(3).ReadOnly = True
End If
End Sub

现在我在最初的问题中包含的代码工作了。

再次感谢@jmcilhinney!

最新更新