数据网格查看问题 [英] Data Grid View question

查看:92
本文介绍了数据网格查看问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让这段代码只接受数字0-7

how do i get this code to accept numbers 0-7 only

If (e.ColumnIndex = 4) Then   ' Checking numeric value for Column 4 only
    Dim value As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
    For Each c As Char In value
        If Not Char.IsDigit(c) Then
            MessageBox.Show("Please enter number 0-7 only.", "Input Error", _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Empty
            Exit Sub
        End If
    Next

推荐答案

尝试:

Try:
For Each c As Char In value
    Dim i As Integer = -1
    If Not (Int32.TryParse(c, i) AndAlso i >= 0 AndAlso i <= 7) Then
        ' your other code here





[更新]



如果你不想分割价值尝试这个:



[UPDATE]

If you don't want to split the value into seperate chars, try this:

Dim i As Integer = -1
If Not (Int32.TryParse(value, i) AndAlso i >= 0 AndAlso i <= 7) Then
    ' your other code here



如果您使用此代码,请勿使用 For Each 循环!


这篇关于数据网格查看问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆