Datagridview 单元格值评估 vb.net

Posted

技术标签:

【中文标题】Datagridview 单元格值评估 vb.net【英文标题】:Datagridview cell value evaluation vb.net 【发布时间】:2016-05-13 13:53:25 【问题描述】:

我正在尝试处理我的 datagridview 单元格数据(特定列的)以首先评估用户输入的数值,然后评估介于 0 和 90 之间的值...此代码在文本框情况下的工作原理几乎相同,但是当我尝试将其应用于 datagridview 单元格情况时,一切都会丢失。我该如何解决这个问题?

    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

【问题讨论】:

【参考方案1】:

你可以处理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

【讨论】:

以上是关于Datagridview 单元格值评估 vb.net的主要内容,如果未能解决你的问题,请参考以下文章

c# DataGridView 从选定行获取单元格值

赢表格。使用 BindingNavigator 时如何使 DataGridView 写入单元格值

组合框单元格值数据源datagridview

C# Winforms DataGridView 选定单元格正在清除单元格值

DataGridView ComboBox Column:从下拉列表中选择后更改单元格值?

C#datagridview在拖放值之前获取当前单元格值