如何在C#Win中创建DataGridViewTextBoxColumn的自定义属性.形式 [英] How to create custom property of DataGridViewTextBoxColumn in c# win. form

查看:637
本文介绍了如何在C#Win中创建DataGridViewTextBoxColumn的自定义属性.形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 DataGridViewTextBoxColumn 中创建名为 IsCheck (值为true/false)的属性?

我在互联网上搜索,但找不到创建财产的解决方案.请帮助我提供一个代码片段.

How can i create a property named IsCheck (value true/false) in DataGridViewTextBoxColumn?

i searched the internet but cannot find a solution to create property. Please help me with a code snippet or so.

推荐答案

我认为您应该像下面这样覆盖DataGridViewTextBoxColumn:

I think you should overrided DataGridViewTextBoxColumn Like as bellow:

Public Class sampleDataGridViewNumberTextBoxColumn
    Inherits System.Windows.Forms.DataGridViewTextBoxColumn
    //Define in here
EndClass



请参阅此链接 [



Pls refer this link[^]

This is sample code to customize GridViewTextboxColumn to GridViewNumberTextboxColumn:

Option Explicit On
Option Strict On
 
Imports System.ComponentModel
Imports System.Windows.Forms
 
Public Class sampleDataGridViewNumberTextBoxColumn
    Inherits System.Windows.Forms.DataGridViewTextBoxColumn
 
    Private _orijinalText As String
    Private _minValue As Decimal
    Private _maxValue As Decimal
    Private _CellValueType As Type
 
    Sub New()
        InitializeComponent()
        Me.DefaultCellStyle.Format = "#,##0"
        Me._orijinalText = ""
        Me._minValue = -999999999
        Me._maxValue = 999999999
        Me._CellValueType = Type.Number
        SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        Dim cell As New sampleDataGridViewDateTextBoxCell()
        Me.CellTemplate = cell
    End Sub
    
    'This is override function
    Public Overrides Function Clone() As Object
        Dim cloneObject As sampleDataGridViewNumberTextBoxColumn = DirectCast(MyBase.Clone, sampleDataGridViewNumberTextBoxColumn)
        cloneObject.MinValue = Me.MinValue
        cloneObject.MaxValue = Me.MaxValue
        cloneObject.CellValueType = Me.CellValueType
        Return cloneObject
    End Function
    
    ''' <summary>
    ''' This is custom function to set max length
    ''' </summary>
    ''' <value></value>
    Private Sub SetMaxLength( _
        ByVal format As String, _
        ByVal minValue As Decimal, _
        ByVal maxValue As Decimal)
       
        Dim formattedMinValue As String = minValue.ToString(format)
        Dim formattedMaxValue As String = maxValue.ToString(format)
        
        If formattedMinValue.Length > formattedMaxValue.Length Then
            MyBase.MaxInputLength = formattedMinValue.Length
        Else
            MyBase.MaxInputLength = formattedMaxValue.Length
        End If
 
    End Sub
 

 
    ''' <summary>
    ''' This is custom property MinValue
    ''' </summary>
    ''' <value></value>
    <defaultvalue(gettype(decimal),> _
    Public Property MinValue() As Decimal
        Get
            Return _minValue
        End Get
        Set(ByVal value As Decimal)
            _minValue = value            
            SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        End Set
    End Property
 
    ''' <summary>
    ''' This is custom property MaxValue
    ''' </summary>
    ''' <value></value>
    <defaultvalue(gettype(decimal),> _
    Public Property MaxValue() As Decimal
        Get
            Return _maxValue
        End Get
        Set(ByVal value As Decimal)
            _maxValue = value            
            SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        End Set
    End Property
    
    'This is override function
    Public Shadows Property DefaultCellStyle() As DataGridViewCellStyle
        Get
            Return MyBase.DefaultCellStyle
        End Get
        Set(ByVal value As DataGridViewCellStyle)
            MyBase.DefaultCellStyle = value
            SetMaxLength(Me.DefaultCellStyle.Format, _minValue, _maxValue)
        End Set
    End Property
 

    ''' <summary>
    ''' MaxInputLength (This is override function)
    ''' </summary>
    <browsable(false)> _
    Public Shadows ReadOnly Property MaxInputLength() As Decimal
        Get
            Return MyBase.MaxInputLength
        End Get
    End Property
    
    Public Enum Type
        Number
        Code
    End Enum
 
End Class
 
Public Class sampleDataGridViewNumberTextBoxCell
    Inherits System.Windows.Forms.DataGridViewTextBoxCell
    'This is override function
    Public Overrides ReadOnly Property EditType() As System.Type
        Get
            Return GetType(sampleDataGridViewNumberTextBoxEditingControl)
        End Get
    End Property
 
End Class
 
Public Class sampleDataGridViewNumberTextBoxEditingControl
    Inherits System.Windows.Forms.DataGridViewTextBoxEditingControl
 
    Private WM_PASTE As Integer = &H302
    'This is override function
    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_PASTE Then
            Dim iData As IDataObject = Clipboard.GetDataObject()
            
            If iData.GetDataPresent(DataFormats.Text) Then
                Dim clipboardString As String = iData.GetData(DataFormats.Text).ToString
                'Acceped only number input
                If Not System.Text.RegularExpressions.Regex.IsMatch(clipboardString, "^\d+


")返回 结束 如果 结束 如果 结束 如果 MyBase .WndProc(m) 结束 结束
") Then Return End If End If End If MyBase.WndProc(m) End Sub End Class



我从互联网上读取了此代码.希望获得帮助.



This code i read from internet.Hope this help .


这篇关于如何在C#Win中创建DataGridViewTextBoxColumn的自定义属性.形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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