DataGridView 单元格编辑十进制/十六进制格式的问题

发布于 2024-09-06 01:49:11 字数 1022 浏览 2 评论 0原文

我有一个 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”,但我没有找到它。

I have a DataGridView bound to a DataTable that has 1+16 columns defined as Integer.

The default cell style is hexadecimal 2 digits (.Format="X2").

When entering in cell editing I would like to provide to the user, the possibility to write the value in decimal or hexdacimal.

  1. Hexadecimal could be written like, for example, 0x00, 0X01, x02, XFF
  2. Decimal like 0, 1, 2, 15

For this reason in EditingControlShowing I add "0x" to the TextBox value

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

while in TextBox_KeyPress sub I perform all input filtering to avoid invalid inputs.

What I cannot understand is to which event may I attach to detect when the editing is finished. I would like something opposite to EditingControlShowing so that I can remove "0x" but I didn't found it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-09-13 01:49:11

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

CellParsing

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

   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

After try all possible events both in TextBox and in DataGRidView I finally found one useful for my case.

CellParsing

I copy my code maybe it could help someone else :)

   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
<逆流佳人身旁 2024-09-13 01:49:11

我将使用 gridView 操作事件 CellValueChanged。如果为时已晚,请使用 CellValueChanging

I would use the gridView actionevent CellValueChanged. If that is too late then use CellValueChanging

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文