[英]DataGridView Cell Editing issue with decimal/hexadecimal formatting
我有一個綁定到一個DataTable的DataGridView ,該表有1 + 16列定義為Integer 。
默認的單元格樣式是十六進制的2位數字( .Format="X2"
)。
在輸入單元格編輯時,我想向用戶提供以十進制或十六進制寫值的可能性。
因此,在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子目錄中,我執行所有輸入過濾以避免輸入無效。
我無法理解的是,我可以附加哪個事件來檢測編輯何時完成。 我想要與EditingControlShowing相反的內容,以便可以刪除“ 0x”,但沒有找到。
在嘗試了所有可能的事件后,無論是在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
我將使用gridView actionevent CellValueChanged。 如果為時已晚,請使用CellValueChanging
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.