2

我有一个DataGridView绑定到一个DataTable,它有 1+16 列定义为Integer

默认单元格样式是 2 位十六进制数字 ( .Format="X2")。

在输入单元格编辑时,我想向用户提供以十进制或十六进制写入值的可能性。

  1. 十六进制可以写成,例如,0x00、0X01、x02、XFF
  2. 十进制,如 0、1、2、15

出于这个原因,在EditingControlShowing我将“0x”添加到 TextBox 值

Private Sub BankGrid_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)

    Dim grid As DataGridView = DirectCast(sender, DataGridView)
    If Not TypeOf e.Control Is TextBox Then Return

    Dim tb As TextBox = DirectCast(e.Control, TextBox)
    tb.Text = "0x" & tb.Text

    RemoveHandler tb.KeyPress, AddressOf TextBox_KeyPress
    AddHandler tb.KeyPress, AddressOf TextBox_KeyPress

End Sub

而在TextBox_KeyPress sub 我执行所有输入过滤以避免无效输入。

我无法理解的是我可以附加哪个事件来检测编辑何时完成。我想要与EditingControlShowing相反的东西,以便我可以删除“0x”,但我没有找到它。

4

2 回答 2

1

在尝试了 TextBox 和 DataGRidView 中所有可能的事件之后,我终于找到了一个对我的案例有用的事件。

单元解析

我复制我的代码也许它可以帮助其他人:)

   Private Sub BankGrid_CellParsing(ByVal sender As Object, ByVal e As DataGridViewCellParsingEventArgs)

        Dim grid As DataGridView = DirectCast(sender, DataGridView)
        Dim cell As CustomCell = DirectCast(grid(e.ColumnIndex, e.RowIndex), CustomCell)

        If e.Value Is Nothing OrElse String.IsNullOrEmpty(e.Value.ToString) Then
            e.Value = cell.Value
        Else

            Dim iValue As Integer
            If TryParseNumeric(e.Value.ToString, iValue) Then

                If iValue >= 0 AndAlso iValue <= &HFF Then
                    e.Value = iValue  'value inside the range, accept it'
                Else
                    e.Value = cell.Value 'value outside the range, reload old value'
                End If

            Else                    
                e.Value = cell.Value 'invalid input, reload old value'
            End If

        End If

        e.ParsingApplied = True

    End Sub
于 2010-06-17T14:27:01.210 回答
0

我会使用gridView actionevent CellValueChanged。如果为时已晚,请使用 CellValueChanging

于 2010-06-16T15:34:02.443 回答