【问题标题】:Vb.Net 2013 Datagridview issue on DirectCast TextBoxDirectCast TextBox 上的 Vb.Net 2013 Datagridview 问题
【发布时间】:2017-09-08 18:29:20
【问题描述】:

我有这段代码,如果修改了,比如第一个单元格,然后我点击TextBox,然后再次点击修改过的单元格,尝试在同一个单元格中再次写入,罕见的效果并没有正确写入同一个单元格。 您需要更改行并返回到上一个单元格以在该单元格中回写。

我对以这种方式工作的代码很感兴趣,但这不会产生这种奇怪的效果。

如果我在事件 HandleEcTextChanged (...) 中删除以下代码行

Me.grid.Rows(cell.RowIndex).Cells(Me.lengthColumn.Index).Value = ec.Text.Length

问题没有发生,但我需要以编程方式更新 Length 列。

另外,如果datagridview连接到数据库,你会得到一个错误“数据表的索引已经损坏。”

重现问题的步骤:

  1. 修改datagridview“apple”的第一个单元格。
  2. 单击 TexBox“0000000”。
  3. 点击上面修改过的单元格。
  4. 在当前单元格中输入任意值。
  5. 在前一点出现了问题。不要在单元格中正确书写。

请帮忙。

坦克。

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

【问题讨论】:

  • 你修改了什么代码? (原始代码来自哪里)?另外:Stack Overflow官方语言为英文(其他语言的段落请编辑删除)
  • 请帮助解决这个问题,坦克。

标签: datagridview textbox directcast


【解决方案1】:

使用直接广播访问您的单元格。这是我为访问动态生成的文本框而编写的示例代码。

    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2011-02-10
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多