2016-05-13 107 views
0

我想处理我的datagridview单元格数据(的特定列)来评估用户输入首先为一个数值然后为值介于0和90 ...此代码几乎工作相同的文本框的情况,但当我尝试并将其应用于datagridview单元格的情况一切都会错过..我该如何解决这个问题?datagridview单元格值评估vb.net

Private Sub DataGridView1_EditingControlShowing1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then 
     Dim txtedit As TextBox = DirectCast(e.Control, TextBox) 
     AddHandler txtedit.KeyPress, AddressOf txtEdit_KeyPress 
    End If 
End Sub 

    Private Sub txtEdit_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) 

    If IsNumeric(DataGridView1.CurrentCell.Value) Then 
     'VALIDATES CURRENT CELL INPUT TO VALUES RANGING BETWEEN 1 AND 90°... 
     If DataGridView1.CurrentCell.Value <= 0 Or DataGridView1.CurrentCell.Value >= 91 Then 
      MessageBox.Show("The angle you are trying to achieve is " & DataGridView1.CurrentCell.Value & "°." & vbCrLf & vbCrLf & "Only values ranging from 1° to 90° are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) 
     Else 
      'NOTHING 
     End If 
    Else 
     MessageBox.Show("Only numeric values are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) 
    End If 
End Sub 

回答

0

可以韩德尔DataGridView1_CellEndEdit

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit 

     Try 
      Dim order As DataGridView = DirectCast(sender, DataGridView) 

      If IsNumeric(order("NmeOfColumn", e.RowIndex).Value) Then 

       If order("NmeOfColumn", e.RowIndex).Value <= 0 Or order("NmeOfColumn", e.RowIndex).Value >= 91 Then 
        MessageBox.Show("The angle you are trying to achieve is " & DataGridView1.CurrentCell.Value & "°." & vbCrLf & vbCrLf & "Only values ranging from 1° to 90° are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) 
       Else 
        'NOTHING 
       End If 
      Else 
       MessageBox.Show("Only numeric values are permitted.", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) 
      End If 


     Catch ex As Exception 

     End Try 
    End Sub 
+0

由于多数民众赞成在正确的方向让我:) – TM80

+0

愉快:) –