我有这段代码,如果修改了,例如第一个单元格,然后我单击文本框并再次单击修改后的单元格,尝试在同一个单元格中再次写入,这是一种罕见的效果,并且没有正确写入同一个单元格。您需要更改行并返回到前一个单元格以在该单元格中回写。
我对以这种方式工作的代码很感兴趣,但这不会产生这种奇怪的效果。
如果我在事件 HandleEcTextChanged (...) 中删除以下代码行
Me.grid.Rows(cell.RowIndex).Cells(Me.lengthColumn.Index).Value = ec.Text.Length
问题没有发生,但我需要以编程方式更新 Length 列。
此外,如果 datagridview 连接到数据库,则会收到错误“数据表的索引已损坏”。
重现问题的步骤:
- 修改datagridview“apple”的第一个单元格。
- 单击 TexBox“0000000”。
- 单击上面修改的单元格。
- 在当前单元格中输入任何值。
- 在上一点出现问题。不要在单元格中正确书写。
请帮忙。
坦克。
Public Class TextBoxDirectCast
Private WithEvents table As DataTable
Private WithEvents grid As DataGridView
Private WithEvents textColumn As DataGridViewTextBoxColumn
Private WithEvents lengthColumn As DataGridViewTextBoxColumn
Private WithEvents texboxtext As TextBox
Public Sub New()
Me.InitializeComponent()
Me.ClientSize = New Size(400, 300)
Me.table = New DataTable()
Me.table.Columns.Add("Text", GetType(String))
Me.table.Columns.Add("Length", GetType(Integer))
Me.table.Rows.Add("apple", 5)
Me.table.Rows.Add("banana", 6)
Me.table.Rows.Add("orange", 6)
Me.textColumn = New DataGridViewTextBoxColumn() With {.DataPropertyName = "Text", .HeaderText = "Text", .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill}
Me.lengthColumn = New DataGridViewTextBoxColumn() With {.DataPropertyName = "Length", .ReadOnly = True, .HeaderText = "Length (Computed)", .Width = 200, .MinimumWidth = 200}
Me.grid = New DataGridView() With {.Dock = DockStyle.Top, .AutoGenerateColumns = False, .DataSource = Me.table, .TabIndex = 0}
Me.grid.Columns.AddRange({Me.textColumn, Me.lengthColumn})
Me.Controls.Add(Me.grid)
Me.texboxtext = New TextBox With {.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left, .Text = "0000000", .Location = New Point(10, Me.ClientSize.Height - 30), .TabIndex = 1}
Me.Controls.Add(Me.texboxtext)
Me.texboxtext.BringToFront()
End Sub
Private Sub HandleEcShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles grid.EditingControlShowing
If (Me.grid.CurrentCell.ColumnIndex = Me.textColumn.Index) Then
Dim ec As DataGridViewTextBoxEditingControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
Me.UnhookEc(ec) 'Important: Remove handles to avoid recursion.
Me.HookEc(ec)
End If
End Sub
Private Sub HandleEcTextChanged(sender As Object, e As EventArgs)
Dim ec As DataGridViewTextBoxEditingControl = DirectCast(sender, DataGridViewTextBoxEditingControl)
Dim cell As DataGridViewTextBoxCell = DirectCast(Me.grid.CurrentCell, DataGridViewTextBoxCell)
Me.grid.Rows(cell.RowIndex).Cells(Me.lengthColumn.Index).Value = ec.Text.Length
End Sub
Private Sub HandleEcDisposed(sender As Object, e As EventArgs)
Me.UnhookEc(TryCast(sender, DataGridViewTextBoxEditingControl)) 'Important: This will ensure removal of the hooked handles.
End Sub
Private Sub HookEc(ec As DataGridViewTextBoxEditingControl)
If (Not ec Is Nothing) Then
AddHandler ec.TextChanged, AddressOf Me.HandleEcTextChanged
AddHandler ec.Disposed, AddressOf Me.HandleEcDisposed
End If
End Sub
Private Sub UnhookEc(ec As DataGridViewTextBoxEditingControl)
If (Not ec Is Nothing) Then
RemoveHandler ec.TextChanged, AddressOf Me.HandleEcTextChanged
RemoveHandler ec.Disposed, AddressOf Me.HandleEcDisposed
End If
End Sub
End Class