{
CellEdit = (DataGridViewTextBoxEditingControl)e.Control; // 赋值
CellEdit.KeyPress += Cells_KeyPress; // 绑定到事件
}
{
if (datagridyf.CurrentCellAddress.X == 2) // 判断当前列是不是要控制的列 我是控制的索引值为2的 列(即第三列)
{
if ((Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57) && Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 13)
{
e.Handled = true; // 输入非法就屏蔽
}
else
{
if ((Convert.ToInt32(e.KeyChar) == 46) && (txtjg.Text.IndexOf(".") != -1))
{
e.Handled = true;
}
}
}
}
{
{
datagridyf.Rows [e.RowIndex].ErrorText ="";
int NewVal=0;
if (!int.TryParse (e.FormattedValue.ToString (),out NewVal ) || NewVal <0)
{
e.Cancel=true ;
datagridyf.Rows [e.RowIndex].ErrorText ="价格列只能输入数字";
return ;
}
}
}
下面是VB.NET版本()
Dim WithEvents CellEdit As New DataGridViewTextBoxEditingControl
Private Sub dgvXXX_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvXXX.EditingControlShowing
CellEdit = CType(e.Control, DataGridViewTextBoxEditingControl)
CellEdit.SelectAll()
AddHandler CellEdit.KeyPress, AddressOf Cells_KeyPress
End Sub
' Defect fixing for BEACQ00000345 - EdwardGuo
Private Sub Cells_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If dgvXXX.CurrentCellAddress.X = 1 Then
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
If Not (IsNumeric(e.KeyChar) AndAlso e.KeyChar <> ".") Then
e.Handled = True
End If
End If
End Sub
End Class
评论