仅粘贴datagridviewtextboxcolumn中的数字 [英] Paste only the numbers in datagridviewtextboxcolumn

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

问题描述

我是datagridviewtextboxcolumn只能从键盘输入数字。但是当我通过粘贴输入时,仍然可以输入数字以外的字符。请给我一个解决方案:-0

谢谢

解决方案

< blockquote>您可能需要自定义控件,请查看以下链接

DataGridView的自定义数字编辑元素 [ ^ ]


您可以使用DataGridView的EditingControlShowing事件来创建事件并将其添加到文本框中。你可以添加一个KeyPress事件,KeyUp,KeyDown,TextChanged ....任何东西。您只需要一个具有textbox事件所具有的匹配参数的方法。



例如,以下是我过去使用的仅允许数据网格视图的文本框中的数字和小数点:

 私有  Sub  gvMast_EditingControlShowing( ByVal 发​​件人作为 对象 ByVal  e  As  System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)句柄 dgvCLUs.EditingControlShowing 

选择 案例 e.Control。 GetType
Case GetType (System.Windows.Forms.DataGridViewTextBoxEditingControl)
< span class =code-keyword> Dim txtTemp As TextBox = CType (e.Control,TextBox)

' 在添加处理程序之前删除处理程序以确保事件只运行一次
' RemoveHandler tmp.PreviewKeyDown,AddressOf MyPreviewKeyDown
RemoveHandler txtTemp .KeyPress, AddressOf GridTextbox_NumericWithDecimal_KeyPress
AddHandler txtTemp.KeyPress, AddressOf GridTextbox_NumericWithDecimal_KeyPress

结束 选择

结束 Sub

PRIVA te Sub GridTextbox_NumericWithDecimal_KeyPress( ByVal sender As 系统。对象 ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.Handled = IsNumericWithDecimalKeyPress(e.KeyChar)
结束 Sub

公共 功能 IsNumericWithDecimalKeyPress( ByVal myKeyChar As Char As Boolean
如果 Char .IsDigit(myKeyChar) Char .IsControl(myKeyChar) myKeyChar = 然后
返回 False
否则
返回 True
结束 如果
结束 功能


I'm datagridviewtextboxcolumn can only enter numbers from the keyboard. But when I enter through the paste, characters other than numbers can still be entered.Please give me a solution:-0
Thank You

解决方案

you may need custom control for that, check below link
Custom Numeric Edit Elements for DataGridView[^]


You can use the DataGridView's EditingControlShowing event to create and add an event to the textbox. You can add a KeyPress event, KeyUp, KeyDown, TextChanged....anything. You just need a method that has the matching arguments that the textbox event has.

For example, here is what I've used in the past to only allow numeric and decimal points in the textbox of a datagridview:

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

      Select Case e.Control.GetType
           Case GetType(System.Windows.Forms.DataGridViewTextBoxEditingControl)
               Dim txtTemp As TextBox = CType(e.Control, TextBox)

               'Remove the handler before adding it to ensure that the event will only be run once
               'RemoveHandler tmp.PreviewKeyDown, AddressOf MyPreviewKeyDown
               RemoveHandler txtTemp.KeyPress, AddressOf GridTextbox_NumericWithDecimal_KeyPress
               AddHandler txtTemp.KeyPress, AddressOf GridTextbox_NumericWithDecimal_KeyPress

       End Select

   End Sub

    Private Sub GridTextbox_NumericWithDecimal_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = IsNumericWithDecimalKeyPress(e.KeyChar)
    End Sub

    Public Function IsNumericWithDecimalKeyPress(ByVal myKeyChar As Char) As Boolean
        If Char.IsDigit(myKeyChar) Or Char.IsControl(myKeyChar) Or myKeyChar = "." Then
            Return False
        Else
            Return True
        End If
    End Function


这篇关于仅粘贴datagridviewtextboxcolumn中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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