1。如何在DateGrid中录入一行数据后,按回车转如下一行
2。如何在转入下一行前判断输入的数据是否合法,如录入的数据是否 >0 and <100

解决方案 »

  1.   

    if Cint(datagrid.text)>0 and Cint(datagrid.text)<100 then row=row+1
      

  2.   

    Dim strOld As StringPrivate Sub DataGrid1_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
        strOld = OldValue
    End SubPrivate Sub DataGrid1_BeforeUpdate(Cancel As Integer)
        If DataGrid1.Text < 0 Or DataGrid1.Text > 10 Then
            MsgBox "数据非法~"
            DataGrid1.Text = strOld
            'Cancel = True
        End If
    End Sub
      

  3.   

    Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = 13 Then DataGrid1.Row = DataGrid1.Row + 1
    End Sub
      

  4.   

    dim gtext as string
    gtext=..........'这行中的某数据
    if Cint(gtext)>0 and Cint(gtext)<100 then 
      datagrid.row=datagrid.row+1
    else 
      msgbox"您刚才输入的数据有误,请重新输入!",,"提示"
    end if
      

  5.   


    Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
       If Cint(datagrid1.text)>0 and Cint(datagrid2.text)<100 and keycode=vbKeyReturn Then
          DataGrid1.Row = DataGrid1.Row + 1
          DataGrid1.Col = DataGrid1.Col
       End If
    End Sub
      

  6.   


    Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
       If KeyCode = 13 Then 
         If DataGrid1.Text < 0 Or DataGrid1.Text > 100 Then
            MsgBox "数据输入有误!"
            Exit Sub        
         End If
         DataGrid1.Row = DataGrid1.Row + 1
       End If
    End Sub
      

  7.   

    Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
       If KeyCode = 13 Then 
         If DataGrid1.Text < 0 Or DataGrid1.Text > 100 Then
            MsgBox "数据输入有误!"
            Exit Sub        
         End If
         DataGrid1.Row = DataGrid1.Row + 1
         DataGrid1.Col = 0
       End If
    End Sub
      

  8.   

    谢谢各位,如下写可以解决我的问题。但是仍有两个BUG
    1。输入错误信息时,如果用鼠标选择时,判断不起作用。如果用事件BeforeColUpdate可以对任何的变换单元格进行判断,但是用回车时,第一次焦点停留在原地,再按一次报DataGrid1.Row = DataGrid1.Row + 1出错  实时错误‘7011’
    2。用KeyDown事件需按两次回车,不知是什么原因Private Sub datagrid1_KeyPress(Keycode As Integer)
       If Keycode = 13 Then
         If CInt(DataGrid1.Text) < 0 Or CInt(DataGrid1.Text) > 100 Then
           MsgBox "数据输入出错&iexcl;"
           Exit Sub
         End If
         DataGrid1.Row = DataGrid1.Row + 1
        
       End If
    End Sub
      

  9.   

    要鼠标起作用,须在mouse_down,mouse_move,mouse_up中添代码