DataGridView 单元格编辑十进制/十六进制格式的问题
我有一个 DataGridView 绑定到 DataTable,其中 1+16 列定义为 Integer。
默认单元格样式为十六进制 2 位数字 (.Format="X2"
)。
当进入单元格编辑时,我想向用户提供以十进制或十六进制写入值的可能性。
- 十六进制可以写成,例如 0x00, 0X01, x02, XFF
- 十进制可以写成 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.
- Hexadecimal could be written like, for example, 0x00, 0X01, x02, XFF
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试了 TextBox 和 DataGRidView 中所有可能的事件之后,我终于找到了一个对我的情况有用的事件。
CellParsing
我复制我的代码也许它可以帮助其他人:)
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 :)
我将使用 gridView 操作事件 CellValueChanged。如果为时已晚,请使用 CellValueChanging
I would use the gridView actionevent CellValueChanged. If that is too late then use CellValueChanging