zoukankan      html  css  js  c++  java
  • DataGridView 只能输入整数解决方案

    今天写项目功能点时,遇到一个问题,DataGridViewTextBoxColumn  只能输入数值,并且格式化为 xxxxx.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

  • 相关阅读:
    百度云如何免费扩容至2055G?
    OKR学习总结
    layui和bootstrap 对比
    使用马克飞象+印象笔记 如何简单便捷地发表博客?
    Sublime使用记录之SublimeServer插件介绍
    12.RcolorBrewer包
    11.R语言流程
    25.conda 下载安装与运用
    7、purge_haplogs 基因组去冗余
    5.MCScanX 与circos下载、安装、运用
  • 原文地址:https://www.cnblogs.com/yiwuya/p/1420908.html
Copyright © 2011-2022 走看看