检查数据网格视图中的区域是否包含","如果是,请将其更改为":"



我的应用程序包含一个Datagridview,用户可以输入诸如:天,时间,他工作了多少小时等值。问题是我的第二个应用程序使用此数据进行计算。它必须采用某种格式。就像时间应该是"09:15",但我注意到有些用户正在使用"09,15"。伙计们,你们能帮帮我吗,我需要一段代码来检查 Datagridview 中的范围是否包含一些"列入黑名单的字符",如果是,请用正确的字符替换它。谢谢大家。

不要将值另存为字符串。
直接验证所需类型的输入字符串。
然后,验证的值将传递给第二个应用程序。在这种情况下,您不需要"列入黑名单的字符"

Private Sub DataGridView_CellValidating(sender As Object, 
                                        e As DataGridViewCellValidatingEventArgs)
    If e.RowIndex < 0 Then Exit sub
    Dim dgv As DataGridView = DirectCast(sender, DataGridView)
    Dim columnIndexWithInteger As Integer = 2
    Dim columnIndexWithDate As Integer = 3
    Select Case e.ColumnIndex
        Case columnIndexWithInteger 
            Dim temp As Integer
            If Integer.TryParse(e.FormattedValue, temp) = False Then
                e.Cancel = True
            End If
        Case columnIndexWithDate 
            Dim temp As Date
            If Date.TryParse(e.FormattedValue, temp) = False Then
                e.Cancel = True
            End If
    End Select
End Sub

DataGridView中,你有一个句柄可以让你检查编辑过的单元格的有效性:CellValueValidating 。在考虑用户更改(CellValueChanged事件)之前调用它。

您可以在此处找到示例和解释:- https://msdn.microsoft.com/en-us/library/ykdxa0bc(v=vs.110).aspx- https://msdn.microsoft.com/en-US/library/7ehy30d4(v=vs.110).aspx

您还可以查看CellValueChangedCellValueValidatedCellEndEdit事件。

最新更新