我的程序要在datagrid的第五列中输入内容。我现在想限制输入内容的长度,比如说只能输十个字。
我不知道在哪判断。
Private Sub Command1_KeyPress(KeyAscii As Integer)
    'If Len(DataGrid1.Columns(4).Text) = 5 Then
End Sub
我本想写在keypress事件中,可我发现它根本不执行。
With DataGrid1.Columns(4)
        .Width = 4000
        .AllowSizing = False
        .Button = True
        '.Locked = True
.text
End With
我又想写到那一列的设置里,可text后面又不知道写什么。
我又想,可能是数据库里限制多长,它就能输入多长吧。
也不行,输入可以随便输,一旦超长,就会提示 步操作产生错误。请检查每一步的状态值
我现在想限制那一列的输入长度,应该怎么做啊?

解决方案 »

  1.   


    Private Sub DataGrid1_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
         If ColIndex = 4 Then
            If Len(DataGrid1.Columns(ColIndex).Text) > 5 Then
               MsgBox "内容长度不能大于5个字符"
               Cancel = 1
            End If
         End If
         
    End Sub
      

  2.   

    Private Sub DataGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii As Integer, Cancel As Integer)
         If ColIndex = 4 Then
            If Len(DataGrid1.Columns(ColIndex).Text & Chr(KeyAscii)) > 5 Then
               MsgBox "内容长度不能大于5个字符"
               Cancel = 1
            End If
         End IfEnd Sub
      

  3.   


    猴哥 这个DataGrid1.Columns(ColIndex).Text是旧值,不能起作用
      

  4.   

    Private Sub DataGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii As Integer, Cancel As Integer)
         If ColIndex = 4 Then
            If Len(DataGrid1.Columns(ColIndex).Text & Chr(KeyAscii)) > 5 Then
               MsgBox "内容长度不能大于5个字符"
               Cancel = 1
            End If
         End IfEnd Sub