DataGridView 只能输入整数解决方案
来源:互联网 发布:陶瓷的制作知乎 编辑:程序博客网 时间:2024/06/05 14:59
今天写项目功能点时,遇到一个问题,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
- DataGridView 只能输入整数解决方案
- datagridview 只能输入数字
- dataGridView中只能输入数字
- input 只能输入整数,或者只能输入整数和小数
- 只能输入整数或小数
- 限制EditText只能输入整数
- 限制文本框只能输入整数
- EditText限制只能输入整数
- DataGridView Keypress事件只能输入数字
- datagridview 限制某列只能输入数字
- dataGridView中只能输入数字的判断
- datagridview 限制某列只能输入数字
- datagridview 限制某列只能输入数字
- DataGridView 列大写、列只能输入数字
- DataGridView的单元格控制只能输入数字
- DataGridView的单元格控制只能输入数字
- DataGridView单元格只输入小数或整数
- 只能输入整数的客户端脚本验证
- 用VS做汇编开发环境的方法
- 亿万富翁这样炼成:巴菲特理财习惯揭秘
- secrets of the javascript Ninja (Function Prototypes)(javascript忍者的秘密)
- 足球联赛管理系统所有数据库对象
- 巴菲特名言语录
- DataGridView 只能输入整数解决方案
- 巴菲特价值投资法则
- SQL语法大全
- stat,fstat以及lstat函数
- 常见数据库分页SQL语句
- Head First C# 中文版 图文皆译 第七章 接口和抽象类 page275
- 金沙湾猎豹野战记
- 我心中的敏捷(4)----多样的形式与不变的本质
- Myeclipse注册机源代码