Datagridview only integer solutions can be entered

Source: Internet
Author: User

When writing a project function point today, you may encounter a problem. The datagridviewtextboxcolumn can only be formatted as XXXXX. XX decimal places.

I tried a lot of methods, but I could only verify them after entering them. Then I searched the internet and found a good method. After modification, it was good. Now I want to convert it to VB. net Language, recorded below:

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.