DataGridView 只能輸入整數解決方案

2021-04-25 23:05:05 字數 1722 閱讀 5647

今天寫專案功能點時,遇到乙個問題,datagridviewtextboxcolumn  只能輸入數值,並且格式化為 ***xx.xx 兩位小數.

想了很多方法,但只能是輸入完後才驗證,後來在網上搜了一下,找到了乙個好的方法,經過修改後不錯,現在我把它轉換為vb.net 語言,記錄下來:

private _editcell as datagridviewtextboxeditingcontrol = nothing

private sub dgvservices_editingcontrolshowing(byval sender as system.object, byval e as system.windows.forms.datagridvieweditingcontrolshowingeventargs) handles dgvservices.editingcontrolshowing

if dgvservices.currentcelladdress.x = colservice_rate.index then

_editcell = ctype(e.control, datagridviewtextboxeditingcontrol)

_editcell.selectall()

addhandler _editcell.keypress, new keypresseventhandler(addressof me.editcell_keypress)

end if

end sub

private sub editcell_keypress(byval sender as object, byval e as keypresseventargs)

if ((convert.toint32(e.keychar) < 48 orelse convert.toint32(e.keychar) > 57) andalso convert.toint32(e.keychar) <> 46 andalso convert.toint32(e.keychar) <> 8 andalso convert.toint32(e.keychar) <> 13) then

e.handled = true

else

if ((convert.toint32(e.keychar) = 46) andalso ctype(sender, datagridviewtextboxeditingcontrol).text.indexof(".") <> -1) then

e.handled = true

end if

end if

end sub

private sub dgvservices_cellendedit(byval sender as system.object, byval e as system.windows.forms.datagridviewcelleventargs) handles dgvservices.cellendedit

if e.columnindex = colservice_rate.index andalso e.rowindex > -1 then

dgvservices(e.columnindex, e.rowindex).value = math.round(convert.todecimal(dgvservices(e.columnindex, e.rowindex).value), 2, midpointrounding.awayfromzero)

end if

end sub

Delphi中限制文字框 TEdit 只能輸入數字

delphi中限制文字框 tedit 只能輸入數字 軟體技術 procedure tform1.edit1keypress sender tobject var key char varedt tedit str,strl,strr string p integer begin 獲取當前文字內容,注...

Structured Streaming 輸入輸出

sparksession.readstream 返回乙個datastreamreader介面物件,可以通過該物件對輸入源進行引數配置,最後返回dataframe dataset物件。val csvdf spark readstream option sep schema userschema csv...

橋牌筆記 輸張墊輸張

南主打4h。西拿第一墩梅花後,換攻小方塊。看上去只有3個輸張,2個梅花,1個將牌。但有乙個危險,看來西的方塊是單張。如果莊家馬上掉將,西會用紅桃a拿住,打小梅花到到東家的a上手,東再打回方塊給西家將吃,從而定約下一。如何防止方塊被吃掉?關鍵是要切斷梅花的橋路,採用輸張墊輸張的辦法。西家拿黑桃 的可能...