DirectCast TextBox上的Vb.Net 2013 Datagridview问题 [英] Vb.Net 2013 Datagridview issue on DirectCast TextBox

查看:53
本文介绍了DirectCast TextBox上的Vb.Net 2013 Datagridview问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。
$


我有这个代码,如果修改了,例如第一个单元格,然后我点击TextBox再次点击修改后的单元格,尝试再次在同一个单元格中写入,这是一种罕见的效果,并且未在同一单元格中正确写入。

您需要更改行并返回上一个单元格以在该单元格中写回。



我对代码工作很感兴趣,但这并没有产生这种奇怪的效果。



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



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



问题不会发生,但我需要以编程方式更新长度列。



此外,如果datagridview连接到数据库,则会收到错误"它损坏的数据表的索引"。



重现问题的步骤:

1.修改冷杉datagridview"apple"的st单元格。"
2.单击TexBox"0000000"。

3.单击上面修改的单元格。

4.在当前单元格中输入任何值。

5.在上一点出现问题。不要在单元格中正确写入。



请帮助。



坦克。



公共类TextBoxDirectCast



    Private WithEvents表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 =新尺寸(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()使用{.DataPropertyName =" Text",。HeaderText =" Text",。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill}

        Me.lengthColumn = New DataGridViewTextBoxColumn(){{。DataPropertyName =" Length",。ReadOnly = True,.HeaderText =" Length(Computed)",。Width = 200,。MinimumWidth = 200}¥ b $ b        Me.grid = New DataGridView()使用{.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 =具有{.Anchor = AnchorStyles.Bottom或AnchorStyles.Left,.Text =" 0000000",。Location = New Point(10,Me.ClientSize.Height - 30),。TabIndex = 1}的新TextBox br />
        Me.Controls.Add(Me.texboxtext)

        Me.texboxtext.BringToFront()

   结束分


    Private Sub HandleEcShowing(sender As Object,e As DataGridViewEditingControlShowingEventArgs)处理grid.EditingControlShowing

       如果(Me.grid.CurrentCell.ColumnIndex = Me.textColumn.Index)则为
            Dim ec As DataGridViewTextBoxEditingControl = DirectCast(e.Control,DataGridViewTextBoxEditingControl)

            Me.UnhookEc(ec)'重要提示:删除句柄以避免递归。

            Me.HookEc(ec)

       结束如果

   结束分


    Private Sub HandleEcTextChanged(发件人为对象,e为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

   结束分


    Private Sub HandleEcDisposed(发件人为对象,e为EventArgs)

        Me.UnhookEc(TryCast(sender,DataGridViewTextBoxEditingControl))'重要:这将确保删除钩住的句柄。

   结束分


    Private Sub HookEc(ec As DataGridViewTextBoxEditingControl)

       如果(不是什么都没有)然后

            AddHandler ec.TextChanged,AddressOf Me.HandleEcTextChanged

            AddHandler ec.Disposed,AddressOf Me.HandleEcDisposed

       结束如果

   结束分


    Private Sub UnhookEc(ec As DataGridViewTextBoxEditingControl)

       如果(不是什么都没有)然后

            RemoveHandler ec.TextChanged,AddressOf Me.HandleEcTextChanged

            RemoveHandler ec.Disposed,AddressOf Me.HandleEcDisposed

       结束如果

   结束点¥


结束等级




解决方案

嗨DelfinB,


就像你提到的标题一样,你的案例与Winform  Datagridview更相关,我会将你的案例移到Winform论坛以获得更好的支持。



祝你好运。


Kristin 


Hello.

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.
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 (...)

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."

Steps to reproduce the problem:
1. Modify the first cell of datagridview "apple".
2. Click the TexBox "0000000".
3. Click on the cell modified above.
4. Enter any value in the current cell.
5. In the previous point the problem occurs. Do not write properly in the cell.

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


解决方案

Hi DelfinB,

Like your title mentioned, your case more related to Winform  Datagridview, I will move your case to Winform forum for better support.

Best regards.

Kristin  


这篇关于DirectCast TextBox上的Vb.Net 2013 Datagridview问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆