我有一个数据网格视图绑定到一个数据表1+16 列定义为Integer.
默认单元格样式为十六进制 2 位数字 (.Format="X2"
).
当进入单元格编辑时,我想向用户提供以十进制或十六进制写入值的可能性。
- 十六进制可以写成,例如 0x00、0X01、x02、XFF
- 十进制,如 0、1、2、15
由于这个原因在编辑控件显示我将“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
而在文本框_按键sub 我执行所有输入过滤以避免无效输入。
我无法理解的是,编辑完成后我可以附加到哪个事件来检测。我想要相反的东西编辑控件显示这样我就可以删除“0x”,但我没有找到它。
在尝试了 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)