在vb.net中的datagridview的特定列的单元格中仅键入数字值 [英] Type only numeric value in particular column's cells of datagridview in vb.net

查看:101
本文介绍了在vb.net中的datagridview的特定列的单元格中仅键入数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个datagridview用作dgv1
在其中添加一列作为天"
我希望用户只能在列的单元格中键入数字值.

I had taken one datagridview as dgv1
In that add one column as Day
I want a user can type only numeric value in column''s cells.
how can do that?

推荐答案

您可以设置ajax的过滤器文本.为此.

you can set filter text of ajax. to do that.

<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender4" runat="server" FilterType="Numbers"
                                                            TargetControlID="txtNo">
                                                        </cc1:FilteredTextBoxExtender>


以下内容假定具有datagridview的表单具有一个名为"Day"的列. Form_Load将列设置为ValueType为"Integer".如果输入了非整数,则DataError方法将捕获该错误,然后打开一个MsgBox.我还添加了对Day号的检查,以将其强制为1到31之间-当CellValueChanged方法触发时,检查完成.

公共课程表格1
昏暗如System.Windows.Forms.DataGridViewDataErrorEventArgs
私有子DataGridView1_CellValueChanged(_
ByVal发件人为System.Object,_
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)_
处理DataGridView1.CellValueChanged
如果e.RowIndex> == 0则
如果DataGridView1.Rows(e.RowIndex).Cells("Day").Value< 1 _
或DataGridView1.Rows(e.RowIndex).Cells("Day").Value> 31然后
DataGridView1_DataError(Me,ex)
DataGridView1.CancelEdit()
如果结束
如果结束
结束子
私有子Form1_Load(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
DataGridView1.Columns("Day").ValueType = GetType(Integer)
Show()
结束子
私有子DataGridView1_DataError(_
ByVal发件人为System.Object,_
ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs)_
处理DataGridView1.DataError
MsgBox(发生输入错误.您正在编辑的单元格" _
&仅允许NUMERIC值;请更正.",_
MsgBoxStyle.Information + MsgBoxStyle.OkOnly,糟糕!")
结束子
结束类
The following assumes a form with a datagridview having one column named "Day." The Form_Load sets the column to have a ValueType of "Integer"; if a non-integer is entered, the DataError method catches that and I open a MsgBox. I also added a check for the Day number to force it to be between 1 and 31 -- the check is done when the CellValueChanged method fires.

Public Class Form1
Dim ex As System.Windows.Forms.DataGridViewDataErrorEventArgs
Private Sub DataGridView1_CellValueChanged( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
If e.RowIndex >= 0 Then
If DataGridView1.Rows(e.RowIndex).Cells("Day").Value < 1 _
Or DataGridView1.Rows(e.RowIndex).Cells("Day").Value > 31 Then
DataGridView1_DataError(Me, ex)
DataGridView1.CancelEdit()
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns("Day").ValueType = GetType(Integer)
Show()
End Sub
Private Sub DataGridView1_DataError( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError
MsgBox("An input error occurred. The cell you are editing " _
& "only allows NUMERIC values; please correct.", _
MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Oops!")
End Sub
End Class


这篇关于在vb.net中的datagridview的特定列的单元格中仅键入数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆