Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net DataGridView仅数值单元格?_.net_Vb.net_Winforms_Datagridview - Fatal编程技术网

.net DataGridView仅数值单元格?

.net DataGridView仅数值单元格?,.net,vb.net,winforms,datagridview,.net,Vb.net,Winforms,Datagridview,我是winforms新手。我正在尝试将DataGridView的两列设置为仅数值。。我不希望用户能够在单元格中键入任何内容,除非它在一列中是一个自然数,在另一列中是一个数值(总是一个小数点)。 我以为这很简单。。但即使在stackoverflow和其他网站上尝试了很多东西,我仍然无法做到这一点 If DataGridView1.CurrentCell.ColumnIndex = 8 Then If Not Char.IsControl(e.KeyChar) AndAlso Not Ch

我是winforms新手。我正在尝试将DataGridView的两列设置为仅数值。。我不希望用户能够在单元格中键入任何内容,除非它在一列中是一个自然数,在另一列中是一个数值(总是一个小数点)。 我以为这很简单。。但即使在stackoverflow和其他网站上尝试了很多东西,我仍然无法做到这一点

If DataGridView1.CurrentCell.ColumnIndex = 8 Then

    If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then
        e.Handled = True
    End If

End If 
如果DataGridView1.CurrentCell.ColumnIndex=8,则
如果不是Char.IsControl(e.KeyChar)也不是Char.IsDigit(e.KeyChar)也不是Char.IsDigit(e.KeyChar)也不是e.KeyChar“。c那么
e、 已处理=真
如果结束
如果结束
试试这段代码

 Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

        If DataGridView1.CurrentCell.ColumnIndex = 2 Then

            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress

        ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 Then

            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress1


        End If

    End Sub

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

        If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True

    End Sub

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

        If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True

    End Sub
文本框\仅用于数字的按键事件


TextBox_keyPress1事件用于十进制值的数字

如果只关心数据类型验证,则可以使用如下CellValidating事件

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        //e.FormattedValue  will return current cell value and 
        //e.ColumnIndex & e.RowIndex will rerurn current cell position

        // If you want to validate particular cell data must be numeric then check e.FormattedValue is all numeric 
        // if not then just set  e.Cancel = true and show some message 
        //Like this 

        if (e.ColumnIndex == 1)
        {
            if (!IsNumeric(e.FormattedValue))  // IsNumeric will be your method where you will check for numebrs 
            {
                MessageBox.Show("Enter valid numeric data");
                dataGridView1.CurrentCell.Value = null;
                e.Cancel = true;

            }

        }

    }

下面的代码是Satish解决方案的扩展。它将有助于控制DataGridView单元格的值。Textbox函数仅用于将单元格附加到Textbox事件。无需在DataGridView或表单上的任何位置添加文本框

私有子DataGridView_EditingControlShowing(ByVal发送者作为对象,ByVal e作为System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)处理DataGridView.EditingControlShowing
如果DataGridView.CurrentCell.ColumnIndex=2,则“带小数点的数值列”
AddHandler CType(e.Control,TextBox).KeyPress,TextBox的地址
ElseIf DataGridView.CurrentCell.ColumnIndex=3然后是“不带小数的数值列”
AddHandler CType(e.Control,TextBox).KeyPress,TextBox的地址\u KeyPress 1
ElseIf DataGridView.CurrentCell.ColumnIndex=4则“仅限选定值”
AddHandler CType(e.Control,TextBox).KeyPress,TextBox的地址\u KeyPress 2
ElseIf DataGridView.CurrentCell.ColumnIndex=5然后是“电子邮件列”
AddHandler CType(e.Control,TextBox).KeyPress,TextBox的地址\u KeyPress 3
如果结束
端接头
专用子文本框\按键(ByVal sender作为对象,ByVal e作为按键事件参数)
'允许数值、一个小数点和退格键
将数字标注为Windows.Forms.TextBox=发件人
如果InStr(“1234567890.”,e.KeyChar)=0和Asc(e.KeyChar)8或(e.KeyChar=“.”和InStr(numbers.Text“.”)大于0,则
e、 KeyChar=Chr(0)
e、 已处理=真
如果结束
端接头
专用子文本框_keyPress1(ByVal sender作为对象,ByVal e作为KeyPressEventArgs)
'允许数值,退格键。不允许小数点(即点)
将数字标注为Windows.Forms.TextBox=发件人
如果InStr(“1234567890”,e.KeyChar)=0和Asc(e.KeyChar)8,则
e、 KeyChar=Chr(0)
e、 已处理=真
如果结束
端接头
专用子文本框_keyPress2(ByVal sender作为对象,ByVal e作为KeyPressEventArgs)
'仅允许选定值
如果InStr(“1234567890!@$%^&*()e.KeyChar)大于0,则
e、 KeyChar=Chr(0)
e、 已处理=真
如果结束
端接头
专用子文本框_keypress 3(ByVal sender作为对象,ByVal e作为keypress事件参数)
'Martch函数,需要在类的顶部添加“Imports System.Text.RegularExpressions”
'允许电子邮件值
将电子邮件设置为Windows.Forms.TextBox=发件人
如果是Email.Text“”,则
Dim rex As Match=Regex.Match(Trim(Email.Text),“[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\+[a-zA-Z]{2,3})$”,RegexOptions.IgnoreCase)
如果rex.Success=False,则
MessageBox.Show(“请输入有效的电子邮件地址”,“信息”,MessageBoxButtons.OK,MessageBoxIcon.Information)
Email.BackColor=Color.Red
Email.Focus()
出口接头
其他的
Email.BackColor=Color.White
如果结束
如果结束
端接头

试试这段代码。这与大多数投票答案几乎相同,但我编辑了
KeypressEvent
code使其变得简单,即使您必须输入十进制数字,也可以使用它。享受。:)

Private子DataGridView1_EditingControlShowing(ByVal sender作为对象,ByVal e作为System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)处理DataGridView1.EditingControlShowing
如果DataGridView1.CurrentCell.ColumnIndex=2,则
AddHandler CType(e.Control,TextBox).KeyPress,TextBox的地址
如果结束
端接头
专用子文本框\按键(ByVal sender作为对象,ByVal e作为按键事件参数)
If(非字符)IsControl(例如KeyChar)_
AndAlso(不是字符IsDigit(例如KeyChar)_
AndAlso(e.KeyChar Microsoft.VisualBasic.ChrW(46)))然后
e、 已处理=真
如果结束
端接头
用lambda试试这个

Private Sub dgv_pararelhp_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv_pararelhp.EditingControlShowing
    If dgv_pararelhp.CurrentCell.ColumnIndex = 0 Then'// change this optional your index column.
         AddHandler CType(e.Control, TextBox).KeyPress, Sub(s_, e_)
                                                            If Char.IsDigit(CChar(CStr(e_.KeyChar))) = False Then e_.Handled = True
                                                         End Sub
       End If
End Sub

如果有效的话,我也会接受任何c#的答案。。有在线转换器可用。;)重复问题[在按键事件中使特定列仅接受datagridview中的数值][1][1]:使用EditingControlShowing代替CellValidating查看如何清除单元格?我不希望用户能够键入未经数字修改的我的帖子,请检查@Arbaazadded[dataGridView1.CurrentCell.Value=null;]@Arbaaz至少其他答案(SATSON的)可以,为什么不留下任何评论?您应该在所有答案上留下一些评论,以通知他们改进答案,否则您不会有更好的答案。@KingKing您是对的。。对不起。。我现在在那里留下了一条评论。我正在尝试验证
DataGridViewTextBoxCell
我没有使用TextBox。是的。您没有使用texbox。但您可以通过编写textbox事件并在DataGridView1_EditingControlShowing事件中添加处理程序来实现目标。。您无需在表单设计中添加文本框。这是迄今为止我遇到的最好的解决方案。。很抱歉,我花了这么长时间才回复。谢谢然而,只缺少一件事,那就是。。如果是十进制数,则只允许使用单个十进制。@Arbaaz在编辑后格式化十进制单元格,或检查特定单元格是否有de
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As         System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
 End Sub
 Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True
        If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True
        If e.KeyChar = " "c Then e.Handled = False
  End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    If DataGridView1.CurrentCell.ColumnIndex = 2 Then

        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress

    End If

End Sub

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

     If (Not Char.IsControl(e.KeyChar) _
                AndAlso (Not Char.IsDigit(e.KeyChar) _
                AndAlso (e.KeyChar <> Microsoft.VisualBasic.ChrW(46)))) Then
        e.Handled = True
    End If

End Sub
Private Sub DGV_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGV_TimeSheetMain.EditingControlShowing
    '       '*************Allow only Numbers in DataGridView*************
    Dim txtEdit As TextBox = e.Control
    'remove any existing handler
    RemoveHandler txtEdit.KeyPress, AddressOf TextEdit_Keypress
    AddHandler txtEdit.KeyPress, AddressOf TextEdit_Keypress
End Sub

Private Sub TextEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    'Test for numeric value or backspace in first column
    If DGV.CurrentCell.ColumnIndex = 1 Then
        If IsNumeric(e.KeyChar.ToString()) Or e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = False 'if numeric display
        Else
            e.Handled = True  'if non numeric don't display
        End If
    End If
End Sub
Private Sub dgv_pararelhp_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv_pararelhp.EditingControlShowing
    If dgv_pararelhp.CurrentCell.ColumnIndex = 0 Then'// change this optional your index column.
         AddHandler CType(e.Control, TextBox).KeyPress, Sub(s_, e_)
                                                            If Char.IsDigit(CChar(CStr(e_.KeyChar))) = False Then e_.Handled = True
                                                         End Sub
       End If
End Sub