怎么控制datagridview里的单元格输入格式只能输入手机号
2个回答
展开全部
正则表达式可以,
还有一种方法,就是判断:在datagridview的.keypress事件中判断是否是数字键(手机号一般是11位),不是数字键就禁掉
例如:(这是vb2005的代码,不知道你用的是哪个,但程序空巧代码意思基本相同,只是书写方式不一样,亲桥睁,相信你能斗消键懂)
Public EditCell As DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
EditCell = CType(e.Control, DataGridViewTextBoxEditingControl)
EditCell.SelectAll()
AddHandler EditCell.KeyPress, AddressOf Cells_KeyPress
End Sub
Private Sub Cells_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs)
If e.KeyChar <> Chr(8) And e.KeyChar <> Chr(13) And (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) Then
Beep()
Beep()
e.KeyChar = Chr(0)
End If
End Sub
如果是C#,我这里给你一个:DataGridView中的单元格中只能输入数字的一种解决方法 :就是给DataGirdView添加CellParsing方法
private void dataGridView5_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
if (!reg1.IsMatch(e.Value.ToString()))
{
MessageBox.Show("对不起!必须是数字,请重新输入!");
e.Value = "0.00";
e.ParsingApplied = true;
}
}
你可以试试,祝你成功!
还有一种方法,就是判断:在datagridview的.keypress事件中判断是否是数字键(手机号一般是11位),不是数字键就禁掉
例如:(这是vb2005的代码,不知道你用的是哪个,但程序空巧代码意思基本相同,只是书写方式不一样,亲桥睁,相信你能斗消键懂)
Public EditCell As DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
EditCell = CType(e.Control, DataGridViewTextBoxEditingControl)
EditCell.SelectAll()
AddHandler EditCell.KeyPress, AddressOf Cells_KeyPress
End Sub
Private Sub Cells_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs)
If e.KeyChar <> Chr(8) And e.KeyChar <> Chr(13) And (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) Then
Beep()
Beep()
e.KeyChar = Chr(0)
End If
End Sub
如果是C#,我这里给你一个:DataGridView中的单元格中只能输入数字的一种解决方法 :就是给DataGirdView添加CellParsing方法
private void dataGridView5_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
if (!reg1.IsMatch(e.Value.ToString()))
{
MessageBox.Show("对不起!必须是数字,请重新输入!");
e.Value = "0.00";
e.ParsingApplied = true;
}
}
你可以试试,祝你成功!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询