[英]Vb.Net 2013 Datagridview issue on DirectCast TextBox
我有此代碼,如果進行了修改(例如第一個單元格),那么我單擊TextBox,然后再次單擊修改后的單元格,以嘗試在同一單元格中再次寫入,這種情況很少見,並且無法正確地寫入同一單元格中。 您需要更改行並返回到上一個單元格以在該單元格中寫回。
我對這種方式的代碼工作很感興趣,但這並沒有產生這種奇怪的效果。
如果我在事件HandleEcTextChanged(...)中刪除了以下代碼
Me.grid.Rows(cell.RowIndex).Cells(Me.lengthColumn.Index).Value = ec.Text.Length
該問題不會發生,但是我需要以編程方式更新“長度”列。
另外,如果datagridview連接到數據庫,則會收到錯誤“該數據表的索引已損壞”。
重現此問題的步驟:
請幫助。
戰車
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
使用直接廣播訪問您的單元。 這是我編寫的用於訪問動態生成的文本框的示例代碼。
Dim txt = New TextBox()
txt.Name = "name1"
txt.Size = New Size(200, 70)
txt.Location = New Point(40, 40)
txt.Text = "I am a new textbox"
Me.Controls.Add(txt)
DirectCast(Me.Controls("name1"), TextBox).Text = "Some stuff here" 'The magic happens here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.