[英]Vb.Net 2013 Datagridview issue on DirectCast TextBox
I have this code, if modified, for example the first cell, then I click on the TextBox and click again on the modified cell, to try writing again in the same cell, a rare effect and does not properly written in the same cell. 我有此代码,如果进行了修改(例如第一个单元格),那么我单击TextBox,然后再次单击修改后的单元格,以尝试在同一单元格中再次写入,这种情况很少见,并且无法正确地写入同一单元格中。 You need to change row and return to the previous cell to write back in that cell. 您需要更改行并返回到上一个单元格以在该单元格中写回。
I am interested in the code work this way, but that does not make this weird effect. 我对这种方式的代码工作很感兴趣,但这并没有产生这种奇怪的效果。
If I remove the following line of code in the event HandleEcTextChanged (...) 如果我在事件HandleEcTextChanged(...)中删除了以下代码
Me.grid.Rows(cell.RowIndex).Cells(Me.lengthColumn.Index).Value = ec.Text.Length Me.grid.Rows(cell.RowIndex).Cells(Me.lengthColumn.Index).Value = ec.Text.Length
the problem does not happen, but I need to update the Length column programmatically. 该问题不会发生,但是我需要以编程方式更新“长度”列。
In addition, if the datagridview is connected to a database, you get an error "The index of the datatable it is damaged." 另外,如果datagridview连接到数据库,则会收到错误“该数据表的索引已损坏”。
Steps to reproduce the problem: 重现此问题的步骤:
Help please. 请帮助。
Tanks. 战车
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
Use directcast to access your cells. 使用直接广播访问您的单元。 Here's an example code I wrote to access a dynamically generated textbox. 这是我编写的用于访问动态生成的文本框的示例代码。
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.